Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 23, 2020 21:03
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/0e042523d9ff3c649a1af6a4e6edcbb5 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/0e042523d9ff3c649a1af6a4e6edcbb5 to your computer and use it in GitHub Desktop.
Just PHP FUN 005.
<?php
# https://www.codewars.com/kata/5e4e8f5a72d9550032953717 List of all Rationals.
function allRationals() {
$q = [1,1];
$i = 0;
while(true){
$a = array_shift($q);
$b = array_shift($q);
// echo "$a/$b/n";
yield [$a,$b];
array_push($q,$a,$a+$b);
array_push($q,$a+$b,$b);
}
}
function allRationals() {
$q = [[1,1]];
$i = 0;
while(true){
$x = array_shift($q);
// echo "$x[0]/$x[1]\n";
yield $x;
list($a,$b) = $x;
array_push($q,[$a, $a+$b]);
array_push($q,[$a+$b,$b]);
$i++;
}
}
function take($n, $g) {
$r = [];
foreach ($g as $e) {
$r[] = $e;
if (!--$n) break;
}
return $r;
}
class MyTestCases extends TestCase {
private $a;
public function __construct() {
$this->a = take(20000, allRationals());
}
public function testSimpleTests() {
$this->assertEquals([1, 1], $this->a[0]);
$this->assertEquals([1, 3], $this->a[3]);
$this->assertEquals([3, 2], $this->a[4]);
$this->assertEquals([5, 2], $this->a[10]);
}
public function testLargerIndices() {
$this->assertEquals([19, 12], $this->a[100]);
$this->assertEquals([39, 28], $this->a[1000]);
$this->assertEquals([205, 162], $this->a[10000]);
//$this->assertEquals([713, 586], $this->a[100000]);
}
}
<?php
# https://www.codewars.com/kata/534ea96ebb17181947000ada Breaking chocolate problem.
function breakChocolate ($n, $m) {
if(0 == $n || 0 == $m) return 0;
if(1 == $n && 1 == $m) return 0;
$a = [$n,$m]; sort($a);
list($n,$m) = $a;
if( 1 == $n) return $m - 1;
return ($n - 1) + $n *($m - 1);
};
<?php
# https://www.codewars.com/kata/56e7d40129035aed6c000632 Easy Line.
function easyline($n) {
$x = array_fill(0,$n+1,1);
$x = array_map(function($y) use($n) {return array_fill(0,$n+1,0);}, $x);
$x[0][0] = 1;
$x[1][0] = 1;
$x[1][1] = 1;
for($i = 2; $i <= $n ; $i++){
$x[$i][0] = 1;
for($j = 1; $j < $i; $j++){
$x[$i][$j] = $x[$i-1][$j - 1] + $x[$i-1][$j];
}
$x[$i][$i] = 1;
}
$z = $x[$n];
$z = array_map(
function($x){ return floatval($x*$x);},
$z
);
return round(log(array_sum($z)));
}
<?php
# https://www.codewars.com/kata/570409d3d80ec699af001bf9 The fusc function -- Part 1.
function fusc(int $n): int {
if( 0 == $n) return 0;
if( 1 == $n) return 1;
if(0 == $n%2) return fusc($n/2);
$x = ($n - 1) / 2;
return fusc($x) + fusc($x + 1);
}

Just PHP FUN 005.

Started and 23.05.2020 21:03 Satruday May.
Started at 24.05.2020 01:00 Sunday May. Finished at 24.05.2020 04:03 Sunday May.

<?php
# https://www.codewars.com/kata/588422ba4e8efb583d00007d Simple Fun #3: Late Ride.
function late_ride(int $n): int {
$hrs = floor($n/60);
$mins = $n%60;
return array_sum(str_split(strval($hrs * 100 + $mins)));
}
<?php
# https://www.codewars.com/kata/55e6f5e58f7817808e00002e A Rule of Divisibility by 7.
function seven($x) {
if($x < 1) return [$x,0];
$step = 1;
$prev = 0;
for($i = 0; $i < 50000; $i++){
$prev = $x;
$x = floor($x/10) - ($x%10)*2;
if( 0 == $x%7 && $x < 100) return [$x, $step];
if( $x < 100) return [$x,$step];
$step += 1;
}
throw new Exception("This could not be here!");
}
<?php
# https://www.codewars.com/kata/511f11d355fe575d2c000001 Two Oldest Ages.
// Return the two oldest/oldest ages within the array of ages passed in.
function twoOldestAges($ages) {
sort($ages);
array_unique($ages);
return array_slice($ages,-2,2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment