Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
//An array is special if every even index contains an even number and every odd index contains an odd number. 
var_dump(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]));//➞ true
var_dump(isSpecialArray([2, 7, 9, 1, 6, 1, 6, 3]));//➞ false
var_dump(isSpecialArray([2, 7, 8, 8, 6, 1, 6, 3]));//➞ false

function isSpecialArray($array){
	foreach($array as $key=>$value){
		if($key%2+$value%2==1){
			return "Array is not special";
		}
	}
	return "Array is special";
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo correctSpacing_1(" my   name   is Slim Shady  ")."\n";
echo correctSpacing_2(" my   name   is Giovani Giorgio  ")."\n";

//Using explode function
function correctSpacing_1($sentence) {
	$array = explode(' ', $sentence);
	$count = count($array);
	for ($i = 0; $i < $count; $i++) {
		if (!$array[$i]) {
			unset ($array[$i]);
		}
	}
	return implode(' ', $array);
}

//Using while loop
function correctSpacing_2($sentence) {
	$sentence = trim($sentence);
	while (str_contains($sentence, '  ')) {
		$sentence = str_replace('  ', ' ', $sentence);
	}
	return $sentence;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
var_dump(isValidHexCode("#CD5C5C")); // ➞ true
var_dump(isValidHexCode("#EAECEE")); // ➞ true
var_dump(isValidHexCode("#eaecee")); // ➞ true
var_dump(isValidHexCode("#CD5C58C")); // ➞ false
// Length exceeds 6
var_dump(isValidHexCode("#CD5C5Z")); // ➞ false
// Not all alphabetic characters in A-F
var_dump(isValidHexCode("#CD5C&C")); // ➞ false
// Contains unacceptable character
var_dump(isValidHexCode("CD5C5C")); // ➞ false
// Missing #


function isValidHexCode($code) {
	$allowed = '0123456789abcdef';
	$len = strlen($code);
	if ($len!=7 || $code[0]!='#') {
		return false;
	}
	for ($i=1; $i<$len; $i++) {
		if (!str_contains($allowed, strtolower($code[$i]))) {
			return false;
		}
	}
	return true;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo century(1756)."\n";
echo century(1555)."\n";
echo century(1000)."\n";
echo century(1001)."\n";
echo century(2005)."\n";


// Get the Century
function century($year) {
	$suf = 'th';
	if ($year > 2000) {
		$suf = 'st';
	}
	return ceil($year/100).$suf.' century';
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
print_r(findPrimeNumbers(50));

//Prime numbers are evenly divided by themselves and by one
//Finds first N prime number
function findPrimeNumbers($n){
	$num=2;//first  prime number is 2
	$res=[];
	while  (0<$n){
		$cnt=0;
		for ($i=2; $i<=$num; $i++){
			if ($num%$i==0){
				$cnt++;
			}
		}
		if ($cnt==1){
			$res[]=$num;
			$n--;
		}
		$num++;
	}
	return $res;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo "6 - ".checkNumber(6)."\n\n";
echo "9 - ".checkNumber(9)."\n\n";
echo "12 - ".checkNumber(12)."\n\n";

function checkNumber($num) {
	$res = 'Deficient';

	if ($num == 1) {
		return false;
	}

	$sqrt = sqrt($num);

	$sum = 1;
	// Loops starting 2 to the square root of the given number
	for ($i = 2; $i <= $sqrt; $i++) {
		if ($num%$i == 0) {
			$sum += $i;

			$result = $num/$i;
			// If $i is not equal to the square root,
			// then adds it to the $sum variable
			if ($result != $i) {
				$sum += $result;
			}
		}
	}
	if ($sum == $num) {
		$res = 'Perfect';
	} else if ($sum > $num) {
		$res = 'Abundant';
	}
	return $res;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
<?php
echo convertKilometersToMiles(56)."\n";
echo convertMilesToKilometers(265.56)."\n";

function convertKilometersToMiles($km){
$mile = 0.621371;
return $km * $mile;
}

function convertMilesToKilometers($miles){
$one_miles = 0.621371;
return $miles * $one_miles;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo libraryFine_3(10, 7, 2015, 7, 7, 2015)."\n\n";
echo libraryFine_3(10, 8, 2015, 5, 3, 2015)."\n\n";
echo libraryFine_3(10, 7, 2017, 5, 7, 2015)."\n\n";
echo libraryFine_3(10, 7, 2017, 10, 7, 2017)."\n\n";

//Using if and else if functions
function libraryFine($d1, $m1, $y1, $d2, $m2, $y2) {
	$hacks = 0; //fine unit hackos
	$a = $d1-$d2;
	$b = $m1-$m2;
	$c = $y1-$y2;

	if ($a == 0 && $b == 0 && $c == 0) {
		$hacks = 0;
	} else {
		if ($a != 0) {
			$hacks = 15*$a;
		}if ($b != 0) {
			$hacks = 500*$b;
		}if ($c != 0) {
			$hacks = 10000;
		}
		return $hacks;
	}
}  //This is my idea, maybe good, maybe not so


//Using switch function
function libraryFine_2($d1, $m1, $y1, $d2, $m2, $y2) {
	$hacks = 0;
	$a = $d1-$d2;
	$b = $m1-$m2;
	$c = $y1-$y2;

	switch ($hacks) {
		case ($a == 0 && $b == 0 && $c == 0):
			$hacks = 0;
			break;
		case ($a != 0):
			$hacks = 15*$a;
			break;
		case ($b != 0):
			$hacks = 500*$b;
			break;
		case ($c != 0):
			$hacks = 10000;
			break;
	}
	return $hacks;
}  // doesn't work

// Calculates the fine, if the book is returned late
function libraryFine_3($d1, $m1, $y1, $d2, $m2, $y2) {
    
    $hacks = 0;
    
    // If the book is returned after the calendar 
    // year, there is a fixed fine of 10,000 Hackos
    if ($y1 > $y2) {
        $hacks = 10000;
    } 
    
    // If the book is returned after the expected return 
    // month but still within the same calendar year
    else if ($m1 > $m2  &&  $y1 == $y2) {
        $hacks = 500 * ($m1 - $m2);
    }
    
    // If the book is returned after the expected return 
    // day but still within the same calendar month and year
    else if ($d1 > $d2  &&  $m1 == $m2  &&  $y1 == $y2) {
        $hacks = 15 * ($d1 - $d2);
    }
    return $hacks;
} //fastets solution
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
print_r(stones(4, 10, 100));

// Compute all possible numbers that might occur on the last stone
function stones($n, $a, $b) {
    
    $res = [];
    
    $n_z_s = $n - 1;
    
    // If the difference between $a and $b is 0, 
    // there will be only one possible answer
    if ($a == $b) {
        return [$n_z_s * $a];
    }
    
    // Needs to choose min and max of the numbers 
    // to loop through the possible answers
    $min = min($a, $b);
    $max = max($a, $b);
    
    // As the pattern shows (from the Explanation) the difference  
    // of the answer's numbers is exactly the same as the difference  
    // between the last two parameters: $a and $b, in this case: 90
    $diff = $max - $min;
    for ($i = $n_z_s*$min;  $i <= $max*$n_z_s;  $i += $diff) {
        $res[] = $i;
    }
    return $res;
}


/*
With differences 10 and 100, all possible series are the following:
0, 10, 20, 30
0, 10, 20, 120
0, 10, 110, 120
0, 100, 110, 120
0, 10, 110, 210
0, 100, 110, 210
0, 100, 200, 210
0, 100, 200, 300
Hence the answer 30 120 210 300
As the pattern shows (from the Explanation) the difference  
of the answer's numbers is exactly the same as the difference  
between the last two parameters: $a and $b, in this case: 90
*/
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo repeatedString("mango", 10);

function repeatedString($string, $number) {
	$ltr = 'a'; //letter function
	$strl = strlen($string);

	$res = substr_count($string, $ltr)*floor($number/$strl);

	$res += substr_count(substr($string, 0, $number%$strl), $ltr);

}
return $res;
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
Results: 1578