Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 29, 2020 11:54
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/3968cdfe8354d38d3562d1456dc0eff4 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/3968cdfe8354d38d3562d1456dc0eff4 to your computer and use it in GitHub Desktop.
Just PHP FUN 062.
<?php
# https://www.codewars.com/kata/581e50555f59405743001813 Fun with lists: anyMatch + allMatch.
function any_match($head, $p) {
while($head){
if($p($head->data)) return true;
$head = $head->next;
}
return false;
}
function all_match($head, $p) {
while($head){
if(!$p($head->data)) return false;
$head = $head->next;
}
return true;
}
<?php
# https://www.codewars.com/kata/5828713ed04efde70e000346 Coding Meetup #5 - Higher-Order Functions Series - Prepare the count of languages.
function count_languages($a) {
return array_count_values(array_column($a,'language'));
}

Just PHP FUN 062.

Started at 16:27 29.07.2020 Wednesday July.
Finished at 18:42 29.07.2020 Wednesday July. (2hrs 15minutes)

<?php
# https://www.codewars.com/kata/581c867a33b9fe732e000076 Fun with lists: lastIndexOf.
function last_index_of($head, $value) {
$index = -1;
for($i=0; $head ; $i+=1, $head = $head->next) if($head->data === $value) $index = $i;
return $index;
}
<?php
# https://www.codewars.com/kata/5806c2f897dba05dd900004c N-Dimensional Vector Magnitude.
function magnitude($vector) {
return sqrt(array_sum(array_map(fn($x)=>pow($x,2),$vector)));
}
<?php
# https://www.codewars.com/kata/582609930626631a9600003e Median fun fun.
function median($a) {
if(empty($a)) return null;
sort($a); $size = count($a); $hlf = floor($size/2);
return 1 == $size%2 ? $a[$hlf] : ($a[$hlf-1] + $a[$hlf])/2;
}
@lbvf50mobile
Copy link
Author

<?php
# https://www.codewars.com/kata/5552101f47fc5178b1000050 Playing with digits
function digPow($n, $p) {
  $x1 = 0;
  foreach(str_split(strval($n)) as $v){ $x1 += pow($v,$p); $p += 1;}
  if(0 !== $x1%$n) return -1;
  return $x1/$n;
}

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