Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 6, 2020 18:45
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/f2005cdeeaaabe1b80a8accf546e0d1e to your computer and use it in GitHub Desktop.
Save lbvf50mobile/f2005cdeeaaabe1b80a8accf546e0d1e to your computer and use it in GitHub Desktop.
Just PHP FUN 017.
<?php
# https://www.codewars.com/kata/56f253dd75e340ff670002ac Composing squared strings.
function compose($s1, $s2) {
$a = explode("\n",$s1);
$b = array_reverse(explode("\n",$s2));
$size = count($a);
$answer = "";
for($i = 0; $i < $size; $i++)
$answer .= substr($a[$i],0,$i+1).substr($b[$i],0,$size - $i)."\n";
return trim($answer);
}
<?php
# https://www.codewars.com/kata/569218bc919ccba77000000b Target Date.
function ($a0, $a, $p) {
$p = $p/36000;
$date = new DateTime('2016-01-01');
while($a0 < $a){
$a0 += $a0*$p;
$date->modify('+1 day');
}
return $date->format('Y-m-d');
}
<?php
# https://www.codewars.com/kata/54ff0d1f355cfd20e60001fc Factorial.
function factorial(int $n): int {
if(0 > $n || 12 < $n) throw new RangeException("Incorrect input");
if(0 == $n) return 1;
return array_product(range(1,$n));
}
<?php
# https://www.codewars.com/kata/56e3cd1d93c3d940e50006a4 How Green Is My Valley?
function makeValley($a) {
$x = $a; rsort($x);
$i = 0; $j = count($a) - 1;
for($k = 0; $k < count($a); $k+=1){
if(0 == $k%2){
$a[$i] = $x[$k]; $i+=1;
}
else{
$a[$j] = $x[$k]; $j -= 1;
}
}
return $a;
}
<?php
# https://www.codewars.com/kata/56a4872cbb65f3a610000026 Rotate for a Max.
function maxRot($n) {
$arr = str_split($n);
$s = count($arr);
$max = $n;
for($i = 0; $i < $s; $i +=1){
$shift = $arr[$i];
for($j = $i+1; $j < $s; $j +=1) $arr[$j-1] = $arr[$j];
$arr[$s-1] = $shift;
$tmp = (int) implode($arr);
if($tmp > $max) $max = $tmp;
}
return $max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment