Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
plusMinus([-3, -5, 6, 7, 0, 0, 2, 4, -5, 3]);
//plusMinus([2 ,1,0, 0, 0, -6 ,-4, 10, 3, -7])."\n\n";
//plusMinus([1, 2, -6, -3, 8, 0 , 0, 1,-9, 4])."\n\n";


//calculate a ratio of positive, negative and 0 numbers in array
function plusMinus($arr){
	$r=[0,0,0];
	
	foreach ($arr as $item){
		if ($item>0){
			$r[0]++;
		}else if ($item<0){
			$r[1]++;
		}else{
			$r[2]++;
		}
	}
	//number of items in entire array
	$s = count($arr);
    // Calculates each ratio and rounds the result to 6 digits
    $res = [round($r[0]/$s, 6), round($r[1]/$s, 6), round($r[2]/$s, 6)];
    
    echo $res[0] . "\n";
    echo $res[1] . "\n";
    echo $res[2] . "\n";
	
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
0
echo viralAdvertising(5)."\n\n";
echo viralAdvertising(10);
/* Day Shared Liked Cumulative 1 5 2 2 2 6 3 5 3 9 4 9 4 12 6 15 5 18 9 24 */ // Determines how many people have liked // the ad by the end of a given day
function viralAdvertising($n) {
	$liked = 2;
	$shared = 0;
	$cumulative = 2;
//Looping through the days
for ($i = 2; $i <= $n; $i++) {
		$shared = $liked*3;
		$liked = floor($shared/2);
		$cumulative += $liked;
	}
	return $cumulative;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Note
php
Problem
PHP official doc
0
floor and ceil
floor() ბრძანება ამრგვალებს ნაკლებობით და აბრუნებს უდიდეს მთელ რიცხვს, რომელიც ნაკლებია ათწილადზე. მაგ: 3,45=>3; ceil() ბრძანება ამრგვალებს მეტობით და აბრუნებს უმცირეს მთელ რიცხვს, რომელიც მეტია ათწილადზე. მაგ: 3,45=>4;
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Note
php
PHP official doc
0
echo timeInWords(5, 45)."\n";
echo timeInWords(3, 00)."\n";
echo timeInWords(7, 15)."\n";
echo timeInWords(6, 30)."\n";
echo timeInWords(2, 50)."\n";
echo timeInWords(8, 01)."\n";
echo timeInWords(9, 59)."\n";
//Using else if function
function timeInWords($h, $m) {
	$res = (" ");

	if ($m == 00) {
		$res = $h . " o'clock";
	} else if ($m == 30) {
		$res = "Half past " . $h;
	} else if ($m == 15) {
		$res = "Quarter past " . $h;
	} else if ($m == 45) {
		$res = "Quarter to " . $h + 1;
	} else if ($m < 30) {
		$res = $m . " minutes  past " . $h;
	} else {
		$res = 60 - $m . " minutes  to " . $h + 1;
	}
	return $res;
}
//Using switch function
function timeInWords_2($h, $m) {
	$res = (' ');

	switch ($m) {
		case '00';
			$res = $h . " o'clock";
			break;
		case '30';
			$res = "Half past " . $h;
			break;
		case '45';
			$res = "Quarter to " . $h + 1;
			break;
		case '15';
			$res = "Quarter past " . $h;
			break;
		case $m < 30;
			$res = $m . " minutes  past " . $h;
			break;
		case $m > 30;
			$res = 60 - $m . " minutes  to " . $h + 1;
			break;
	}
	return $res;
}
//Both work, second solution is mine
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
echo utopianTree(5)."\n";
//Using for loops
function utopianTree($n) {
	$val = 1;

	for ($i = 0; $i < $n; $i++) {
		if ($i%2 == 0) {
			//doubled for each odd year
			$val *= 2;
		} else {
			//increased by one for each even year
			$val++;
		}
	}
	return $val;
}
by ვაჟა ტყემალაძე
1 year ago
1
PHP
Problem Solving
Note
php
Problem
0
//The word is an isogram, if a key given in this word is repeated more than ONCE
echo isIsogram("Password")."\n";
echo isIsogram_2("Tkemaladze")."\n";
//Using isset function
function isIsogram($str){
	$str=strtolower($str);//turn to lower case
	$len=strlen($str);//count lenght of word array
	$array=[];//create new array
	for ($i=0; $i<$len; $i++){
		if (!isset($array[$str[$i]])){
			$array[$str[$i]]=true;
		}else{
			return false;
		}
	}
	return true;
}
//usinf in-array function
function isIsogram_2($str){
	$str=strtolower($str);//turn to lower case
	$len=strlen($str);//count lenght of word array
	$array=[];//create new array
	for ($i=0; $i<$len; $i++){
		if (in_array($str[$i], $array)){
			return true;
		}else{
			$array[]=$str[$i];
		}
	}
	return true;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
Array to XML with SimpleXMLElement CODE
Built-in class
SimpleXMLElement
converts array to XML
$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);

$xml = new SimpleXMLElement('<rootTag/>');
to_xml($xml, $test_array);
print $xml->asXML();

function to_xml(SimpleXMLElement $object, array $data)
{   
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $new_object = $object->addChild($key);
            to_xml($new_object, $value);
        } else {
            // if the key is an integer, it needs text with it to actually work.
            if ($key != 0 && $key == (int) $key) {
                $key = "key_$key";
            }

            $object->addChild($key, $value);
        }   
    }   
}
by Valeri Tandilashvili
1 year ago
0
PHP
0
print_r(fizzBuzz_2(35));
function fizzBuzz_1($n){
	$result = [];
	for ($i = 1; $i <= $n; $i++) {
		$output = "";
		if ($i%3 == 0 && $i%5 == 0) {
			$output = "FizzBuzz";
		}if ($i%3 == 0) {
			$output = "Fizz";
		}if ($i%5 == 0) {
			$output = "Buzz";
		}if (!$output) {
			$output = $i;
		}
		$result[] = $output;
	}
}
// The solution is faster
function fizzBuzz_2($n) {
    $result = [];
    for ($i = 1;  $i <= $n;  $i++) {

        if ($i%15 == 0) {
            $output = "FizzBuzz";
        } else if ($i%3 == 0) {
            $output = "Fizz";
        } else if ($i%5 == 0) {
            $output = "Buzz";
        } else {
            $output = "$i";
        }
        $result[] = $output;
    }
    return $result;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
0
<?php

print_r(removeDuplicate_1([2,4,7,5,7,4,1]))."\n";
print_r(removeDuplicate_1([3,5,0,0,1,2,6,6]))."\n";
//Using array_unique function
function removeDuplicate_1($nums){
	return array_unique($nums);
}
function removeDuplicates_2($nums){
	$nums=[];
	foreach ($nums as $num){
		if (!in_array($num,$result)){
			$result[]=$num;
		}
	}
	return $result;
}
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
<?php
//moving zeros to the enda and remainig orders of remaining elements //input:[0,1,0,3,12]=>output:[1,3,12,0,0]
function moveZeroes(&$nums) {
	print_r($nums);
	$count = count($nums); //counting array elements
	for ($i = 0; $i < $count; $i++) {
		//counting loops while situated in array
		if ($nums[$i] == 0) {
			//checking if number is 0
			unset($nums[$i]); //unsetting this number and moving to the end
			$nums[] = 0;
		}
	}
}
$array = [0, 1, 0, 3, 12];
moveZeroes($array);
print_r($array);
by ვაჟა ტყემალაძე
1 year ago
0
PHP
Problem Solving
Note
php
Problem
0
Results: 1578