Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #!/bin/env perl6
  2.  
  3. my @a = 1 .. 13;
  4.  
  5. my @lists = fair-lists(@a, 3);
  6.  
  7. say @$_ for @lists;
  8.  
  9. sub fair-lists ( @array, $num-lists )
  10. {
  11. my $num-elements = @array.elems;
  12. my $num-large = $num-elements % $num-lists; # remainder
  13. my $num-small = $num-lists - $num-large;
  14.  
  15. my $small-size = floor($num-elements/$num-lists);
  16. my $large-size = $small-size + 1; # same as ceiling($num-element/$num-lists);
  17.  
  18. my $first-large = ($num-small * $small-size);
  19. my $last-small = $first-large - 1;
  20.  
  21. my @first-sublist = @array[ 0 .. $last-small];
  22. my @second-sublist = @array[ $first-large .. $num-elements - 1 ];
  23.  
  24. my @small-lists = @first-sublist.rotor($small-size);
  25. my @large-lists = @second-sublist.rotor($large-size);
  26. my @all-lists = (@small-lists, @large-lists).flat;
  27. return @all-lists;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement