Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 8, 2020 15:44
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/8f3764ab6b8f1615d9f19a46130fd0ef to your computer and use it in GitHub Desktop.
Save lbvf50mobile/8f3764ab6b8f1615d9f19a46130fd0ef to your computer and use it in GitHub Desktop.
Just PHP FUN 097.
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment