Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 3, 2020 17: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/9460b6fe3a45c4fb7b6b72ab4ad4119e to your computer and use it in GitHub Desktop.
Save lbvf50mobile/9460b6fe3a45c4fb7b6b72ab4ad4119e to your computer and use it in GitHub Desktop.
# Just PHP FUN 066.
<?php
# https://www.codewars.com/kata/5616868c81a0f281e500005c Prize Draw.
function rank($st, $we, $n) {
if(empty($st)) return "No participants";
$st = explode(',',$st);
if(count($st) < $n) return "Not enough participants";
$som = som($st);
$weight = total_weight(array_map(null,$som,$we));
$pairs = array_map(null,$weight,$st);
echo log1($pairs);
usort($pairs,function($a,$b){
echo "$a[1] ($a[0]) and $b[1] ($b[0])\n";
if($a[0] == $b[0]){
echo "! Check $a[1] and $b[1] \n";
if($a[1] == $b[1]) return 0;
return ($a[1] > $b[1]) ? 1 : -1;
}
return -$a[0] + $b[0];
});
echo log1($pairs);
return $pairs[$n-1][1];
}
function log1($x){ return implode(',',array_map(function($x){return "$x[1]:$x[0]";},$x))."\n";}
function number($x){ return ord($x) - ord('a') + 1;}
function weight_word($x){ return strlen($x) + array_sum(array_map('number',str_split(strtolower($x))));}
function som($x){ return array_map('weight_word',$x);}
function total_weight($x){ return array_map(function($x){return $x[0]*$x[1];},$x);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment