Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 4, 2020 19:34
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/c116da0d995df2779fbbcb44bc89dcfc to your computer and use it in GitHub Desktop.
Save lbvf50mobile/c116da0d995df2779fbbcb44bc89dcfc to your computer and use it in GitHub Desktop.
Just PHP FUN 015.
<?php
# https://www.codewars.com/kata/5d2659626c7aec0022cb8006 The Baum-Sweet sequence.
function baumSweet() {
$ans = [];
for($i = 0; $i <= 1000000; $i += 1){
if(0 == $i){ $ans[$i] = 1; continue;}
$ans[$i] = binary($i);
}
$counter = 0;
while(true){
yield $ans[$counter];
$counter += 1;
}
}
function binary($x){
$str = decbin($x);
$cnt = 0;
for($i = 0; $i < strlen($str); $i++){
if("0" == $str[$i]){
$cnt += 1;
}else{
if($cnt > 0 && 1 == $cnt%2) return 0;
$cnt = 0;
}
}
if($cnt > 0 && 1 == $cnt%2) return 0;
return 1;
}

Just PHP FUN 015.

Started at 20:57 04.06.2020 Thursday June.
Finished at 2:23 05.06.2020 Thursday June. (5hrs 26minutes).

<?php
# https://www.codewars.com/kata/5d26721d48430e0016914faa The PaperFold sequence.
function paperFold() {
$ans = [1];
$size = 1;
while($size <= 1000000){
$tmp = array_map(function($x){ return 1 == $x ? 0 : 1;},array_reverse($ans));
$ans = array_merge($ans,[1],$tmp);
$size = count($ans);
}
$counter = 0;
while(true){
yield $ans[$counter];
$counter += 1;
}
}
<?php
# https://www.codewars.com/kata/51f2b4448cadf20ed0000386 Remove anchor from URL.
function replaceAll($string) {
return preg_replace('/#.*$/','',$string);
}
<?php
# https://www.codewars.com/kata/589573e3f0902e8919000109 Simple Fun #87: Shuffled Array.
function shuffledArray($arr){
$sum = array_sum($arr);
$i = array_search($sum/2,$arr);
$a = array_slice($arr,0,$i);
$b = array_slice($arr,$i+1);
$answer = array_merge($a,$b);
sort($answer);
return $answer;
}
<?php
# https://www.codewars.com/kata/56d904db9963e9cf5000037d Thinking & Testing: A and B?
function testit($a,$b)
{
return $a|$b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment