Started at 23:51 03.08.2020 Aug.
Last active
August 3, 2020 17:30
-
-
Save lbvf50mobile/9460b6fe3a45c4fb7b6b72ab4ad4119e to your computer and use it in GitHub Desktop.
# Just PHP FUN 066.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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