Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 22, 2020 16:30
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/cf7921c6011d269045337b2ee5425558 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/cf7921c6011d269045337b2ee5425558 to your computer and use it in GitHub Desktop.
Just PHP FUN 056.
<?php
# https://www.codewars.com/kata/58856a06760b85c4e6000055 Bits Battle.
function bits_battle($numbers) {
echo "#######\ninput: ".implode(',',$numbers)."\n";
$cnt = function($x){ $a = decbin($x); $b = substr_count($a,'1'); return [-(strlen($a) - $b), $b];};
$res = array_reduce($numbers, function($accum,$x) use ($cnt){
if(0 == $x) return $accum;
$bin = decbin($x);
$parity = 0 == $x%2 ? "even use ZEROs" : "ODD use Ones";
list($zer,$one) = $cnt($x);
$prv = $accum;
$used = $cnt($x)[$x%2];
$accum += $cnt($x)[$x%2];
#echo "$x ($bin) $parity ($zer, $one) $prv + $used = $accum; \n";
return $accum;},0);
if(0 == $res) return 'tie';
if(0 < $res) return 'odds win';
return 'evens win';
}
#--------------------------------------------------------------------------
function bits_battle($numbers) {
$cnt = function($x){ $a = decbin($x); $b = substr_count($a,'1'); return [-(strlen($a) - $b), $b];};
$res = array_reduce($numbers, function($accum,$x) use ($cnt){ return $accum += $cnt($x)[$x%2];},0);
if(0 == $res) return 'tie';
if(0 < $res) return 'odds win';
return 'evens win';
}
<?php
# https://www.codewars.com/kata/5862eeeae20244d5eb000005 Thinkful - Number Drills: Rømer temperature.
function celsius_to_romer($temp) {
return $temp*21/40 + 7.5;
}

Just PHP FUN 056.

Started at 20:30 22.07.2020 Wednesday July.
Finished at 23:29 22.07.2020 Wednesday July. (2hrs 59minutes)

<?php
# https://www.codewars.com/kata/587b6a5e8726476f9b0000e7 Rotational energy levels.
function rot_energies(int $b, int $j_min, int $j_max): array {
if($j_min > $j_max) return [];
if($b <= 0) return [];
$ans = [];
while( $j_min <= $j_max) {
$e = $b * $j_min * ($j_min + 1);
array_push($ans, $e);
$j_min += 1;
}
return $ans;
}
<?php
# https://www.codewars.com/kata/571965ccdf7fdb10a00000ea Sum and Length.
function sum_length($arr) {
$sum = 0; $number = 0; $zer = 0;
foreach($arr as $i=>$v){
if(0 === $v){
if(0 === $zer%2){ $number += 1;}
$zer += 1;
}else{
if(0 < $v){ $sum += $v;}
else{ $number += 1;}
}
}
return "$sum $number";
}
<?php
# https://www.codewars.com/kata/57add740e298a7a6d500000d T.T.T.32: Count with your fingers.
function which_finger($n) {
$finger = 0;
$i = 1;
$d = 1;
for( ; $i < $n; $i +=1){
$finger += $d;
if($finger > 4){
$finger = 3;
$d = -1;
}
if($finger < 0){
$finger = 1;
$d = +1;
}
}
$names = ['Thumb','Index finger','Middle finger','Ring finger','Little finger'];
return $names[$finger];
}
#--------------------------------------------------------------------------------------------------
function which_finger($n) {
$fingers = [
0,1,2,3,4,
3,2,1,0,
1,2,3,4,
3,2,1,0,
1,2,3,4
];
# curr poss
$finger = 0;
$i = 1;
$d = 1;
$cnt = 0;
while($i < $n){
if((0 <= $cnt && $cnt <= 3) || (5 <= $cnt && $cnt <= 7) || (9 <= $cnt && $cnt <= 11) || (13 <= $cnt && $cnt <= 15) || (17 <= $cnt && $cnt <= 19) ){
$finger += $d;
$cnt += 1;
$i += 1;
continue;
}
if(4 == $cnt || 8 == $cnt || 12 == $cnt || 16 == $cnt || 20 == $cnt){
$d = -$d;
$finger += $d;
$cnt = 20 == $cnt ? 0 : $cnt + 1;
$i +=1;
continue;
}
}
$names = ['Thumb','Index finger','Middle finger','Ring finger','Little finger'];
return $names[$finger];
}
# ----------------------------------------------------------------------------------------------------
function which_finger($n) {
$fingers = [
0,1,2,3,4,
3,2,1,0,
1,2,3,4,
3,2,1,0,
1,2,3,4
];
$names = ['Thumb','Index finger','Middle finger','Ring finger','Little finger'];
return $names[$fingers[($n-1)%21]];
}
@lbvf50mobile
Copy link
Author

77,78,177,154,114,93,76,74,182,195,130,196,188,32,156,137,13,172,120,142,9,165,94,3,26,21,128,27,58,169,14,136,46,191,89,161,83,166,34,65,160,165,60,148,198,15,85,10,188,5,153,197,170,46,0,197,67,128,23,126,97,38,61,143,28,151,104,112,117,138,177,77,103,36,25,100,52,110,110,39,115,63,36,85,109,37,82,177,165,106,103,62,144,165,4,172,115,108,83,32

@lbvf50mobile
Copy link
Author

class MyTestCases extends TestCase {  
    public function testBasic() {
      $this->assertEquals('tie', bits_battle([77,78,177,154,114,93,76,74,182,195,130,196,188,32,156,137,13,172,120,
                                              142,9,165,94,3,26,21,128,27,58,169,14,136,46,191,89,161,83,166,34,65,160,165,
                                              60,148,198,15,85,10,188,5,153,197,170,46,0,197,67,128,23,126,97,38,61,143,28,151,104,
                                              112,117,138,177,77,103,36,25,100,52,110,110,39,115,63,36,85,109,37,82,177,165,106,103,62,144,165,
                                              4,172,115,108,83,32]));
      $this->assertEquals('odds win', bits_battle([5, 3, 14]));
      $this->assertEquals('evens win', bits_battle([3, 8, 22, 15, 78]));
      $this->assertEquals('tie', bits_battle([]));
      $this->assertEquals('tie', bits_battle([1, 13, 16]));
    }
}

@lbvf50mobile
Copy link
Author

function which_finger($n) {
 
  $finger = 0;
  $i = 1;
  $d = 1;
  
  for( ; $i < $n; $i +=1){
    $finger += $d;
    if($finger > 4){
      $finger = 3;
      $d = -1;
    }
    if($finger < 0){
      $finger = 1;
      $d = +1;
    }
  }
    
    
  $names =  ['Thumb','Index finger','Middle finger','Ring finger','Little finger'];
  return $names[$finger];
  
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment