<?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;
}
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
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
*/
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;
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";
}
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;
}
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 mineecho 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;
}
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;
}