Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 18, 2020 16:02
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/649a2f4200a8fe75a4feeeb63f2ca368 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/649a2f4200a8fe75a4feeeb63f2ca368 to your computer and use it in GitHub Desktop.
Just PHP FUN 027.
<?php
# https://www.codewars.com/kata/596f6385e7cd727fff0000d6 Average Array.
function avgArray($arr) {
return array_map(function($x){ return array_sum($x)/count($x);},array_map(null,...$arr));
}
# Example #15 Using ... to provide arguments
# https://www.php.net/manual/en/functions.arguments.php
# --------------------------------------------------------------------
function avgArray($arr) {
return array_map(function ($e) {
return array_sum($e) / count($e);
}, array_map(null, ...$arr));
}
# Dzienny.
# Example #4 Performing a zip operation of arrays https://www.php.net/manual/en/function.array-map.php
# --------------------------------------------------------------------
function avgArray($arr) {
$answer = []; $i = 0;
$row= count($arr[0]); $clm = count($arr);
$x = function($acc, $v) use (&$i){ echo "$i"; return $acc + $v[$i];};
for( $i = 0; $i < $row; $i += 1) $answer[] = array_reduce($arr,$x) / $clm;
return $answer;
}
<?php
# https://www.codewars.com/kata/5b36137991c74600f200001b Kill The Monsters!
function killMonsters($h, $x, $dm) {
$hits = hits($h,$x,$dm); $damage = $hits * $dm; $health = $h - $damage;
if( $h <= $damage) return "hero died";
return "hits: $hits, damage: $damage, health: $health"; // TODO
}
function hits($h,$x,$dm){
if($x <= 3) return 0;
if( $x%3 == 0 ) return floor($x/3) - 1;
return floor($x/3);
}
<?php
# https://www.codewars.com/kata/57f609022f4d534f05000024 Find the stray number.
function stray($arr)
{
return array_reduce($arr,function($acc,$x){ return $acc ^ $x;});
}
<?php
# https://www.codewars.com/kata/5390bac347d09b7da40006f6 Jaden Casing Strings.
use function ucwords as toJadenCase;
# technid, mcamuzat, amatoenot, ekzor, TechAddict, mestourbs (plus 2 more warriors)
# ----------------------------------------------------------------------------
function toJadenCase($string)
{
return ucwords($string);
}
# brocsky, zemo, zitttz, sunlanchang, jason321987, Kolin (plus 1731 more warriors)
# ----------------------------------------------------------------------------
function toJadenCase($string)
{
return implode(' ', array_map('ucfirst', explode(' ', $string)));
}
<?php
# https://www.codewars.com/kata/55908aad6620c066bc00002a Exes and Ohs.
function XO($s) {
$x = count_chars(strtolower($s),0);
return $x[ord('x')] == $x[ord('o')];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment