Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 25, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/37666fb500940d8dea475f85b388b96d to your computer and use it in GitHub Desktop.
Save lbvf50mobile/37666fb500940d8dea475f85b388b96d to your computer and use it in GitHub Desktop.
Just PHP FUN 059.
<?php
# https://www.codewars.com/kata/5856c5f7f37aeceaa100008e Christmas baubles on the tree.
function baubles_on_tree(int $baubles, int $branches) {
if( 0 === $branches ) return "Grandma, we will have to buy a Christmas tree first!";
$each_will_have = floor($baubles/$branches);
$answer = array_fill(0,$branches,$each_will_have);
for($i = 0; $i < $baubles%$branches; $i += 1) $answer[$i] += 1; # put one more bauble starting from index.
return $answer;
}
<?php
# https://www.codewars.com/kata/58545549b45c01ccab00058c Calculate Meal Total.
function calculate_total(float $subtotal, float $tax, float $tip): float {
$s = $subtotal;
$st = $subtotal*$tax/100;
$stt = $s * $tip/100;
$total = $s + $st + $stt;
$round = round($total,2);
// echo "s=$s, tax = $tax, tip = $tip; $s + $st + $stt = $total = $round;\n";
return $round;
}

Just PHP FUN 059.

Sarted at 21:59 25.07.2020 Saturday.
Finished at 23:21 25.07.2020 Saturday. (1hr 22minutes)

<?php
# https://www.codewars.com/kata/56f7493f5d7c12d1690000b6 Calculate mean and concatenate string.
function mean(array $a): array {
echo "Input ".implode(',',$a)."\n";
$str = implode(array_filter($a,function($x){ return preg_match('/[a-z]/i',$x);}));
echo "Str: $str\n";
$num = array_map(function($x){ return intval($x);},array_filter($a,function($x){ return preg_match('/[0-9]/',$x);}));
echo "Num: ".implode(',',$num)."=>".array_sum($num)/count($num)."\n";
$x = array_sum($num)/count($num);
return [ count($num) > 10 ? 2*$x : $x,$str];
}
<?php
# https://www.codewars.com/kata/57fb142297e0860073000064
# Exclamation marks series #13: Count the number of exclamation marks and question marks, return the product.
function product(string $s): int {
return substr_count($s,"!") * substr_count($s,"?");
}
<?php
# https://www.codewars.com/kata/584466950d3bedb9b300001f How many times does it contain?
function stringCounter($inputS,$charS){
return substr_count($inputS,$charS);
}
@lbvf50mobile
Copy link
Author

Input 0,6,2,2,6,5,9,4,10,4,8,7,0,1,3,1,9,9,3,9
Str:
Num: 0,6,2,2,6,5,9,4,10,4,8,7,0,1,3,1,9,9,3,9

@lbvf50mobile
Copy link
Author

https://www.codewars.com/kata/56f7493f5d7c12d1690000b6 Calculate mean and concatenate string.

Input 9,2,6,8,9,1,5,7,5,4,10,2,4,8,1,6,0,1,3,4
Str: 
Num: 9,2,6,8,9,1,5,7,5,4,10,2,4,8,1,6,0,1,3,4=>4.75
Failed asserting that two arrays are equal.
Expected: Array (
    0 => 9.5
    1 => ''
)
Actual  : Array (
    0 => 4.75
    1 => ''
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment