- https://www.codewars.com/kata/5d2d0d34bceae80027bffddb Sort Strings by Most Contiguous Vowels.
Started at 21:23 08.09.2020 Tuesday Sep.
Funished at 22:43 08.09.2020 Tuesday Sep. (1hr 20minutes)
Started at 21:23 08.09.2020 Tuesday Sep.
Funished at 22:43 08.09.2020 Tuesday Sep. (1hr 20minutes)
<?php | |
# https://www.codewars.com/kata/5d2d0d34bceae80027bffddb Sort Strings by Most Contiguous Vowels. | |
function sortStringsByVowels($strings){ | |
$get_number = function($s){ | |
$matches = []; | |
$found = preg_match_all('/[aeiouAEIOU]+/',$s,$matches); | |
return $found ? max(array_map('strlen',$matches[0])) : 0; | |
}; | |
$x = array_map(fn($x) => [$x,$get_number($x)],$strings); | |
$hsh = []; | |
foreach($x as $v){ | |
$value = $v[0]; $number = $v[1]; | |
if(!isset($hsh[$number])) $hsh[$number] = [[],$number]; | |
array_push($hsh[$number][0],$value); | |
} | |
$x = array_values($hsh); | |
usort($x,fn($a,$b) => $b[1] - $a[1]); | |
$answer = []; | |
foreach($x as $v){ | |
$answer = array_merge($answer,$v[0]); | |
} | |
return $answer; | |
} | |
# Does not supports stable sort. | |
function sortStringsByVowels($strings){ | |
$get_number = function($s){ | |
$matches = []; | |
$found = preg_match_all('/[aeiouAEIOU]+/',$s,$matches); | |
return $found ? max(array_map('strlen',$matches[0])) : 0; | |
}; | |
$x = array_map(fn($x) => [$x,$get_number($x)],$strings); | |
usort($x,fn($a,$b) => $b[1] - $a[1]); | |
var_dump($strings); | |
var_dump($x); | |
$x = array_map(fn($x)=>$x[0],$x); | |
var_dump($x); | |
return $x; | |
} |
https://stackoverflow.com/questions/4353739/preserve-key-order-stable-sort-when-sorting-with-phps-uasort