fork download
  1. use 5.012;
  2. use warnings;
  3. use List::Util qw(sum);
  4.  
  5. sub hundred {
  6. my @list = @_;
  7. my $i = 0;
  8. return sub {
  9. my $start = $i;
  10. ($start < @list) or return ();
  11. my $sum = 0;
  12. while(($i < @list) and (($sum += $list[$i]) <= 100)){
  13. $i++;
  14. }
  15. return @list[$start .. $i - 1];
  16. };
  17. }
  18.  
  19. my @a = map{ int(rand(10)) + 1 } (1 .. 50);
  20. say "@a";
  21.  
  22. my $h = hundred(@a);
  23. while((my @list = $h->()) > 0){
  24. say "@{[sum(@list)]} : @list";
  25. }
  26.  
Success #stdin #stdout 0s 4888KB
stdin
Standard input is empty
stdout
2 9 2 4 10 9 3 10 6 10 4 9 9 9 1 3 10 5 8 4 2 4 4 8 3 7 1 4 8 1 4 6 6 10 1 5 5 3 2 1 1 1 1 9 7 9 8 5 4 10
100 : 2 9 2 4 10 9 3 10 6 10 4 9 9 9 1 3
96 : 10 5 8 4 2 4 4 8 3 7 1 4 8 1 4 6 6 10 1
71 : 5 5 3 2 1 1 1 1 9 7 9 8 5 4 10