Created
August 3, 2017 19:20
-
-
Save sidneisimeao/10094b6cb52a99feb1476e0becc1162c to your computer and use it in GitHub Desktop.
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 | |
/** | |
Intervalos | |
Você está resolvendo este problema. | |
Este problema foi utilizado em 164 Dojo(s). | |
Dado uma lista de números inteiros, agrupe a lista em um conjunto de intervalos | |
Exemplo: | |
Entrada: 100, 101, 102, 103, 104, 105, 110, 111, 113, 114, 115, 150 | |
Saída: [100-105], [110-111], [113-115], [150] | |
* | |
* ****************Resolvido com PHP Funcional | |
*/ | |
$interval = 5; | |
$input = range(1, 150); | |
$groupIn = function($list) use ($interval) { | |
return array_chunk($list, $interval); | |
}; | |
$groupList = function(array $args, $interval) use ($groupIn) { | |
return($groupIn($args, $interval)); | |
}; | |
$list = function(array $args) { | |
return '[' . min($args) . '-' . max($args) . '],'; | |
}; | |
$group = $groupList($input, $interval); | |
$walkInterval = function(callable $function) use ($group) { | |
$output = ''; | |
foreach ($group as $list) { | |
$output .= $function($list); | |
} | |
return $output; | |
}; | |
print $walkInterval($list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment