Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env perl6
- my @a = 1 .. 13;
- my @lists = fair-lists(@a, 3);
- say @$_ for @lists;
- sub fair-lists ( @array, $num-lists )
- {
- my $num-elements = @array.elems;
- my $num-large = $num-elements % $num-lists; # remainder
- my $num-small = $num-lists - $num-large;
- my $small-size = floor($num-elements/$num-lists);
- my $large-size = $small-size + 1; # same as ceiling($num-element/$num-lists);
- my $first-large = ($num-small * $small-size);
- my $last-small = $first-large - 1;
- my @first-sublist = @array[ 0 .. $last-small];
- my @second-sublist = @array[ $first-large .. $num-elements - 1 ];
- my @small-lists = @first-sublist.rotor($small-size);
- my @large-lists = @second-sublist.rotor($large-size);
- my @all-lists = (@small-lists, @large-lists).flat;
- return @all-lists;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement