Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 15, 2020 16:42
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/b062731c0d4d96f215ac183750b7b742 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/b062731c0d4d96f215ac183750b7b742 to your computer and use it in GitHub Desktop.
Just PHP FUN 050.
<?php
# https://www.codewars.com/kata/589422431a88082ea600002a Simple Fun #75: Digit Degree.
function digit_degree($n) {
$sum = function($x){ return array_sum(str_split($x));};
$cnt = 0;
while($sum($n) != $n){ $cnt += 1; $n = $sum($n);}
return $cnt;
}
<?php
# https://www.codewars.com/kata/5886f3713a111b620f0000dc Simple Fun #28: Html End Tag By Start Tag.
function html_end_tag_by_start_tag(string $start_tag): string {
$x = [];
preg_match('/<([^\s>]+)/',$start_tag,$x);
return '</'.$x[1].'>';
}

Just PHP FUN 050.

Started at 21:42 15.07.2020 Wednesday July.
Finished at 23:42 15.07.2020 Wendesday July. (2hrs 00minutes)

<?php
# https://www.codewars.com/kata/58a3c1f12f949e21b300005c Simple Fun #124: Lamps.
function lamps(array $a): int {
// count if first shine. even == 1, odd == 0
$cnt_even = 0;
// count if first off. even == 0, odd == 1
$cnt_odd = 0;
foreach($a as $i=>$x){
if( 0 == $i % 2){
if(0 == $x) $cnt_even += 1;
if(1 == $x) $cnt_odd += 1;
}else{
if(0 == $x) $cnt_odd += 1;
if(1 == $x) $cnt_even += 1;
}
}
return min($cnt_even,$cnt_odd);
}
<?php
# https://www.codewars.com/kata/5886da134703f125a6000033 Simple Fun #24: Pages Numbering with Ink.
function pages_numbering_with_ink(int $current, int $number_of_digits): int {
while( 0 <= $number_of_digits - floor(log10($current)) - 1){
$number_of_digits -= floor(log10($current)) + 1;
$current += 1;
}
return $current - 1;
}
<?php
# https://www.codewars.com/kata/58846d50f54f021d90000012 Simple Fun #17: Rounders.
function rounders(int $value): int {
echo "Start: $value \n";
while(!preg_match('/^[^0]0*$/',$value)){
$z = preg_replace('/^.*([^0]0*)$/','$1',$value);
// echo "z = $z \n";
$value -= (int) $z;
if($z[0] >= 5){
$z = 10**strlen($z);
//echo "Addin z = $z \n";
$value += $z;
}
echo "$value \n";
}
return $value;
}
class RoundersTest extends TestCase {
public function testBasic() {
$this->assertEquals(20, rounders(15));
$this->assertEquals(1000, rounders(1234));
$this->assertEquals(2000, rounders(1445));
$this->assertEquals(10, rounders(14));
$this->assertEquals(100, rounders(99));
$this->assertEquals(10, rounders(10));
$this->assertEquals(70000000, rounders(69019657));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment