codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
<?php $a = array(8, 16, 16, 32, 8, 8, 4, 4); $group_limit = 32; $current_group = $result = array(); $cycles_since_successful_operation = 0; while ($a && $cycles_since_successful_operation < count($a)) { array_push($current_group,array_shift($a)); if (array_sum($current_group) > $group_limit) array_push($a,array_pop($current_group)); elseif (array_sum($current_group) < $group_limit) $cycles_since_successful_operation = 0; elseif (array_sum($current_group) == $group_limit) { $result []= $current_group; $current_group = array(); $cycles_since_successful_operation = 0; } } if ($a) $result []= $a; // Remaining elements form the last group var_dump($result);
Private
[
?
]
Run code
Submit