Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 10, 2020 16:29
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/1793cb224212cc14d6741ac013cd1958 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/1793cb224212cc14d6741ac013cd1958 to your computer and use it in GitHub Desktop.
Just PHP FUN 020.
<?php
# https://www.codewars.com/kata/5506b230a11c0aeab3000c1f Deodorant Evaporator.
function evaporator($content, $evap_per_day, $threshold) {
$day = 0;
$border = $content * ( $threshold/ 100);
while($content >= $border){
$day += 1;
$content = $content - $content * ($evap_per_day/100);
}
return $day;
}
<?php
# https://www.codewars.com/kata/5500d54c2ebe0a8e8a0003fd Greatest common divisor.
function mygcd($x, $y) {
echo "Input $x and $y \n";
$max = max($x,$y);
$min = min($x,$y);
return g($max,$min);
# https://youtu.be/JUzYl1TYMcU
}
function g($max, $min){
# max = min * (max/min) + max%min
$r = $max % $min;
echo "$max = $min * ".intval($max/$min)." + $r \n";
if(0 == $r) return $min;
return g($min,$r);
}
<?php
# https://www.codewars.com/kata/5d376cdc9bcee7001fcb84c0 Odd Ones Out!
function odd_ones_out($arr) {
$hist = array_count_values($arr);
$rmv = function($x) use ($hist){ return 0 == $hist[$x]%2; };
return array_values(array_filter($arr,$rmv));
}
# ---------------------------------------------------------------------
function odd_ones_out($arr) {
$hist = [];
foreach($arr as $x){
if(!isset($hist[$x])) $hist[$x] = 0;
$hist[$x] += 1;
}
$remove_oddes = function($x) use ($hist){
return 0 == $hist[$x]%2;
};
return array_values(array_filter($arr,$remove_oddes));
}
<?php
# https://www.codewars.com/kata/5cd12646cf44af0020c727dd Square Pi's.
function squarePi($digits){
$row = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
$arr = str_split($row);
for($i = 0, $prev = 0; $i < count($arr); $i +=1 ){
$arr[$i] = pow($arr[$i],2) + $prev;
$prev = $arr[$i];
}
$arr = array_map('sqrt',$arr);
$arr = array_map('ceil',$arr);
return $arr[$digits-1];
}
<?php
# https://www.codewars.com/kata/56d5166ec87df55dbe000063 Sum of Array Averages.
function sumAverage($arr) {
$avg = function($x){ return array_sum($x)/count($x);};
return floor(array_sum(array_map($avg,$arr)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment