Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 11, 2020 19:05
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/27ddccc83bf9ae5f99e098830190225c to your computer and use it in GitHub Desktop.
Save lbvf50mobile/27ddccc83bf9ae5f99e098830190225c to your computer and use it in GitHub Desktop.
<?php
# https://www.codewars.com/kata/57b6f850a6fdc76523001162 Hit Count.
function counter_effect($hit_count) {
return array_map(function($x){ return range(0,$x);}, str_split($hit_count));
}
<?php
# https://www.codewars.com/kata/593f50f343030bd35e0000c6 Ciphers #1 - The 01 Cipher
function encode(string $s): string {
return preg_replace_callback('/[a-z]/i',function($x){
$x = strtolower($x[0]);
return 0 == (ord($x)-ord('a'))%2 ? '0' : '1';
},$s);
}
<?php
# https://www.codewars.com/kata/57aae4facf1fa57b3300005d T.T.T.27: Four piles of apples.
function four_piles(int $n, int $y): array {
$x = $n/(2+$y+1/$y);
$nn = $x+$y + $x-$y + $x*$y + $x/$y;
$x_str = strval($x);
echo "$x $y $n $nn \n";
echo "this is $x_str\n";
if(strstr($x_str,".")){ echo "$x_str has a dot!\n";
return []; }
$x = intval($x_str);
if($x <= $y) {echo "Negative\n"; return [];}
return [$x+$y,$x-$y,$x*$y, $x/$y];
}

Just PHP FUN 047.

Started at 22:29 at 11.07.2020 Saturday July.
Finished at 02:04 at 12.07.2020 Sunday July. ( 3hrs 35mintes)

<?php
# https://www.codewars.com/kata/58b972cae826b960a300003e Jenny the youngest detective.
function missing($nums, $str) {
sort($nums); $str = str_split(str_replace(" ",'',strtolower($str))); $ans = "";
foreach($nums as $v){
if(!isset($str[$v])) return "No mission today";
$ans .= $str[$v];
}
return $ans;
}
<?php
# https://www.codewars.com/kata/58ef87dc4db9b24c6c000092/ Sectional Array Sort.
function sect_sort($array, $start, $length = null) {
echo "input:".implode(",",$array)."\n";
$a = array_slice($array,0,$start);
if($length){
$b = array_slice($array,$start,$length);
$c = array_slice($array,$start+$length);
}else{
$b = array_slice($array,$start);
$c = [];
}
sort($b);
return array_merge($a,array_values($b),$c);
}
@lbvf50mobile
Copy link
Author

Strange behaviour in PHP fractions:
x = 7144 y = 76 n = 557326 nn = 557326 xx = 7144
Fraction

function four_piles(int $n, int $y): array {
  $x = $n/(2+$y+1/$y);
  $nn = $x+$y + $x-$y + $x*$y + $x/$y;
  $xx = floor($x);
  echo "x = $x  y = $y   n = $n  nn = $nn  xx = $xx\n";
  if( floor($x) != $x) {echo "Fraction\n"; return [];}
  if($x <= $y) {echo "Negative\n"; return [];}
  return [$x+$y,$x-$y,$x*$y, $x/$y];
  
}

@lbvf50mobile
Copy link
Author

One more
x = 459 y = 51 n = 24336 nn = 24336 xx = 458
x_str = 459 and xx_str = 458
Fraction

function four_piles(int $n, int $y): array {
  $x = $n/(2+$y+1/$y);
  $nn = $x+$y + $x-$y + $x*$y + $x/$y;
  $xx = floor($x);
  $x_str = strval($x);
  $xx_str = strval($xx);
  echo "x = $x  y = $y  n = $n  nn = $nn xx = $xx \n";
  echo " x_str = $x_str and xx_str = $xx_str \n";
  if( floor($x) != $x) {echo "Fraction\n"; return [];}
  if($x <= $y) {echo "Negative\n"; return [];}
  return [$x+$y,$x-$y,$x*$y, $x/$y];
  
}

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