diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-17 14:49:08 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-17 14:49:08 +0100 |
| commit | bdfe0db0a9735b73e7e99ff50188290e08e1fab3 (patch) | |
| tree | a29868e2357e07369458db58f8b7f776682072a8 | |
| parent | 42228ce601fe37769faacc051f35f74b9566bb26 (diff) | |
| download | perlweeklychallenge-club-bdfe0db0a9735b73e7e99ff50188290e08e1fab3.tar.gz perlweeklychallenge-club-bdfe0db0a9735b73e7e99ff50188290e08e1fab3.tar.bz2 perlweeklychallenge-club-bdfe0db0a9735b73e7e99ff50188290e08e1fab3.zip | |
Fix solution
| -rw-r--r-- | challenge-212/paulo-custodio/perl/ch-2.pl | 32 | ||||
| -rw-r--r-- | challenge-212/paulo-custodio/t/test-2.yaml | 4 |
2 files changed, 14 insertions, 22 deletions
diff --git a/challenge-212/paulo-custodio/perl/ch-2.pl b/challenge-212/paulo-custodio/perl/ch-2.pl index b2af1c85af..c783c3f817 100644 --- a/challenge-212/paulo-custodio/perl/ch-2.pl +++ b/challenge-212/paulo-custodio/perl/ch-2.pl @@ -33,26 +33,18 @@ use Modern::Perl; sub rearrange_groups { - my($count, @nums) = @_; - return -1 unless scalar(@nums) % $count == 0; - my $group_size = scalar(@nums) / $count; - @nums = sort {$a<=>$b} @nums; + my($size, @nums) = @_; + return -1 unless scalar(@nums) % $size == 0; + my %nums; $nums{$_}++ for @nums; my @output; - for my $i (0..$count-1) { + while (%nums) { + my $min = (sort {$a<=>$b} keys %nums)[0]; my @group; - my %seen; - my $j = 0; - while (scalar(@group) < $group_size) { - if (!$seen{$nums[$j]}++) { - push @group, $nums[$j]; - splice(@nums, $j, 1); - } - else { - $j++; - if ($j >= @nums) { - return -1; - } - } + for my $j ($min .. $min+$size-1) { + return -1 unless $nums{$j}; + push @group, $j; + $nums{$j}--; + delete $nums{$j} if $nums{$j}==0; } push @output, \@group; } @@ -75,5 +67,5 @@ sub print_groups { } my @nums = @ARGV; -my $count = pop(@nums); -print_groups(rearrange_groups($count, @nums)); +my $size = pop(@nums); +print_groups(rearrange_groups($size, @nums)); diff --git a/challenge-212/paulo-custodio/t/test-2.yaml b/challenge-212/paulo-custodio/t/test-2.yaml index 2e4b7e2d4c..af3ca6883c 100644 --- a/challenge-212/paulo-custodio/t/test-2.yaml +++ b/challenge-212/paulo-custodio/t/test-2.yaml @@ -10,11 +10,11 @@ output: -1 - setup: cleanup: - args: 1 2 4 3 5 3 2 + args: 1 2 4 3 5 3 3 input: output: (1,2,3), (3,4,5) - setup: cleanup: args: 1 5 2 6 4 7 3 input: - output: (1,2), (4,5), (6,7) + output: -1 |
