Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 27, 2020 15:51
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/bd68d482962eab4d829a2fbb96eb3a5b to your computer and use it in GitHub Desktop.
Save lbvf50mobile/bd68d482962eab4d829a2fbb96eb3a5b to your computer and use it in GitHub Desktop.
Just PHP FUN 008.
<?php
# https://www.codewars.com/kata/57cebe1dc6fdc20c57000ac9 Shortest Word.
function findShort($str){
$matches = [];
preg_match_all('/[a-z]+/',$str,$matches);
return min(array_map('strlen',$matches[0]));
}
<?php
# https://www.codewars.com/kata/5e2733f0e7432a000fb5ecc4 How many urinals are free?
function GetFreeUrinals($urinals){
$count = 0;
if(false !== strpos($urinals,"11")) {
return -1;
}
$arr = str_split($urinals);
$prev = "0";
foreach($arr as $i => $v){
$nxt = $i < count($arr) - 1 ? $arr[$i+1] : "0";
if("1" == $v){
$prev = "1";
}else{
if("0" == $prev && "0" == $nxt ){
$count += 1;
$prev = "1";
}else{
$prev = "0";
}
}
}
return $count;
}

Just PHP FUN 008.

Started at 18:40 27.05.2020 Wednesday May.
Finished at 22:50 27.05.2020 Wednesday May. (3hrs 50minutes)

<?php
# https://www.codewars.com/kata/569b5cec755dd3534d00000f Looking for a benefactor.
function newAvg($arr, $newavg) {
$sum = array_sum($arr);
$n = count($arr);
$answer = $newavg * ($n+1) - $sum;
if($answer <= 0) throw new InvalidArgumentException("Incorrect donation.");
return ceil($answer);
}
<?php
# https://www.codewars.com/kata/58ce8725c835848ad6000007 Drying Potatoes.
function potatoes($p0, $w0, $p1) {
echo "p0=$p0, w0=$w0, p1=$p1\n";
$p0 = $p0 * 0.01; $p1 = $p1 * 0.01;
$a = round($w0*(1-$p0),2);
$b = round(1-$p1,2);
$c = intval($a/$b);
echo "a=$a, b=$b, c=$c\n";
return $c;
}
#--------------------------------------------------------------------
function potatoes($p0, $w0, $p1) {
echo "p0=$p0, w0=$w0, p1=$p1\n";
$p0 = $p0 * 0.01; $p1 = $p1 * 0.01;
$ans = ($w0*(1-$p0))/(1-$p1);
$int = intval(($w0*(1-$p0))/(1-$p1));
$round = round($ans);
$ans_int = intval($ans);
# p0=82, w0=57, p1=81
# ans=54 int=53 ans_int=53 rownd=54 why?
echo "ans=$ans int=$int ans_int=$ans_int round=$round\n";
if($ans == $round) echo "Ans == round\n";
return $int;
}
<?php
# https://www.codewars.com/kata/5b39e3772ae7545f650000fc Remove duplicate words.
function removeDuplicateWords($s) {
return implode(" ",array_unique(explode(" ", $s)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment