Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 13, 2020 18:55
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/cc771da63f7d385867aa937ff7acf70f to your computer and use it in GitHub Desktop.
Save lbvf50mobile/cc771da63f7d385867aa937ff7acf70f to your computer and use it in GitHub Desktop.
Just PHP FUN 075.
<?php
# https://www.codewars.com/kata/56dbf59b0a10feb08c000227 Moves in squared strings (IV)
function rot90Counter($s) {
$arr = str2arr($s); $n = count($arr); $ans = arr_new($n);
for($i = 0; $i < $n; $i += 1) for($j = 0; $j < $n; $j += 1)
$ans[$i][$j] = $arr[$j][$n - $i - 1];
return arr2str($ans);
}
function diag2Sym($s) {
$arr = str2arr($s); $n = count($arr); $ans = arr_new($n);
for($i = 0; $i < $n; $i += 1) for($j = 0; $j < $n; $j += 1)
$ans[$i][$j] = $arr[$n - $j - 1][$n - $i - 1];
return arr2str($ans);
}
function selfieDiag2Counterclock($s) {
$s0 = explode("\n",$s);
$s1 = explode("\n",diag2Sym($s));
$s2 = explode("\n",rot90Counter($s));
return implode("\n",array_map(
function($a,$b,$c){return $a."|".$b."|".$c;}
,$s0,$s1,$s2));
}
function oper($fct, $s) {
return call_user_func($fct,$s);
}
function str2arr($s){ return array_map('str_split',explode("\n",$s));}
function arr_new($n){ return array_map(
function($x) use($n){ return array_fill(0,$n,' ');}
,array_fill(0,$n,' '));}
function arr2str($a){ return implode("\n",array_map('implode',$a));}
<?php
# https://www.codewars.com/kata/56fcc393c5957c666900024d Coding with Squared Strings.
function create_array($n){
return array_map(function($x) use($n){ return array_fill(0,$n,chr(11));},array_fill(0,$n,0));
}
function code($s) {
$n = ceil(sqrt(strlen($s)));
$arr = create_array($n);
$s = str_split($s);
foreach($s as $k=>$v) $arr[floor(($k - $k%$n)/$n)][$k%$n] = $v;
$rotated = create_array($n);
for($i = 0; $i < $n; $i+=1) for($j = 0; $j < $n; $j+=1) $rotated[$i][$j] = $arr[$n - 1 - $j][$i];
return implode("\n",array_map('implode',$rotated));
}
function decode($s) {
$arr = array_map('str_split',explode("\n",$s));
$n = count($arr);
$ans = "";
for($i = 0; $i < $n; $i+=1) for($j = 0; $j < $n; $j+=1) $ans .= $arr[$j][$n - 1 - $i];
return trim($ans,chr(11));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment