Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 25, 2020 16:25
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/cd5e027a4c50b5110e3b774c6b827de8 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/cd5e027a4c50b5110e3b774c6b827de8 to your computer and use it in GitHub Desktop.
Just PHP FUN 033.
<?php
# https://www.codewars.com/kata/59377c53e66267c8f6000027 Alphabet war.
function alphabetWar($fight)
{
$ans = ['Left side wins!', "Let's fight again!", "Right side wins!"];
$hst = count_chars($fight); $lw = 0; $rw = 0;
foreach(['w'=>4,'p'=>3, 'b'=>2, 's'=>1] as $k=>$v) $lw += $v * $hst[ord($k)];
foreach(['m'=>4,'q'=>3, 'd'=>2, 'z'=>1] as $k=>$v) $rw += $v * $hst[ord($k)];
if($lw == $rw) return $ans[1];
if($lw > $rw) return $ans[0];
return $ans[2];
}
<?php
# https://www.codewars.com/kata/586ec0b8d098206cce001141 Thinkful - List and Loop Drills: Inverse Slicer.
function inverse_slice(array $items, int $a, int $b): array {
array_splice($items,$a,$b-$a);
return $items;
}

Just PHP FUN 033.

<?php
# https://www.codewars.com/kata/58844f1a76933b1cd0000023 Simple Fun #8: Kill K-th Bit.
function kill_kth_bit(int $n, int $k): int {
$x = str_split(decbin($n));
if(1 == $x[count($x)-$k]) $x[count($x)-$k] = '0';
return bindec(implode($x));
}
<?php
# https://www.codewars.com/kata/597bb84522bc93b71e00007e String Merge!
function stringMerge($s1, $s2, $l) {
return substr($s1,0,strpos($s1,$l)+1).substr($s2,strpos($s2,$l)+1);
}
<?php
# https://www.codewars.com/kata/57f7796697d62fc93d0001b8 Double Trouble.
function trouble($x, $a)
{
for( $i = 1; $i < count($x); $i += 1)
if($x[$i-1] + $x[$i] == $a){
array_splice($x,$i,1);
$i -= 1;
}
return $x;
}
# ----------------------------------------------------------------------
function trouble($x, $a)
{
echo "Input ".implode(",",$x)." => $a \n";
$l = count($x);
$dlt = []; $ans = []; $cnt = true;
for($s = 1; $s < $l && $cnt ; $s+=1) if($x[$s-1]+$x[$s] == $a) {$dlt[$s] = true; $cnt = false; }
if($cnt) return $x;
echo "Keys for delete".implode(",",array_keys($dlt))." \n";
for($i = 0; $i < $l; $i+=1) if(!isset($dlt[$i])) array_push($ans,$x[$i]);
echo "Return array ".implode(",",$ans)." \n";
return trouble($ans,$a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment