Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 9, 2020 16:36
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/751a3f5b15de75d695ceb2ff1c575910 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/751a3f5b15de75d695ceb2ff1c575910 to your computer and use it in GitHub Desktop.
Just PHP FUN 019.
<?php
# https://www.codewars.com/kata/58841cb52a077503c4000015 Simple Fun #2: Circle of Numbers.
function circle_of_numbers(int $n, int $first_number): int {
return ($first_number + $n/2)%$n;
}
<?php
# https://www.codewars.com/kata/57ee99a16c8df7b02d00045f Flatten and sort an array.
function flatten_and_sort($a) {
$x = [];
array_walk_recursive($a,function($z) use (&$x){ array_push($x,$z);});
sort($x);
return $x;
}

Just PHP FUN 019.

Start at 20:38 09.06.2020 Tuesday June.
Finished at 23:35 09.06.2020 Tuesday June. (2hrs 57minutes)

<?php
# https://www.codewars.com/kata/586909e4c66d18dd1800009b Currying functions: multiply all elements in an array.
/* left blank for unlimited creativity :) */
function multiply_all($array){
return function($x) use($array){
return array_map( function($y) use ($x){
return $x * $y;
},
$array);
};
}
<?php
# https://www.codewars.com/kata/57f8ee485cae443c4d000127 Spacify.
function spacify(string $s): string {
return trim(preg_replace('/./',"$0 ",$s));
}
<?php
# https://www.codewars.com/kata/5a5032f4fd56cb958e00007a TV Remote.
function tvRemote($word) {
// Your code here
$arr = fill();
draw($arr);
$x = 0;
$y = 0;
$sum = 0;
$word = str_split($word);
foreach($word as $char){
list($x1,$y1) = explode(":",$arr[$char]);
$sum += abs($x - $x1) + abs($y - $y1) + 1;
$x = $x1; $y = $y1;
}
return $sum;
}
function fill(){
$ans = [];
arr_fill(range("a","y"),$ans,5,0,0);
arr_fill(range("1","9"),$ans,3,5,0);
arr_fill(str_split(".@0z_/"),$ans,3,5,3);
return $ans;
}
function arr_fill($source, &$dest, $md, $dx, $dy){
foreach($source as $i=>$v){
$x = $i%$md + $dx;
$y = ($i - $i%$md)/$md + $dy;
$dest[$v] = $x.":".$y;
}
}
function draw(&$arr){
$x = array_flip($arr);
for($j = 0 ; $j < 5; $j += 1){
for($i = 0; $i < 8; $i += 1){
echo $x[$i.":".$j];
}
echo "\n";
}
}
@lbvf50mobile
Copy link
Author

array_flip(): Can only flip STRING and INTEGER values!

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