Skip to content

Instantly share code, notes, and snippets.

@sidneisimeao
Created August 3, 2017 19:20
Show Gist options
  • Save sidneisimeao/10094b6cb52a99feb1476e0becc1162c to your computer and use it in GitHub Desktop.
Save sidneisimeao/10094b6cb52a99feb1476e0becc1162c to your computer and use it in GitHub Desktop.
<?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