Skip to content

Commit a75f20a

Browse files
authored
Merge pull request #7464 from jacoby/master
#201 DAJ
2 parents eb45d62 + c5a1407 commit a75f20a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use experimental qw{ say postderef signatures state };
6+
7+
my @examples = (
8+
9+
[ 0, 1, 3 ],
10+
[ 0, 1 ],
11+
[ 1, 3, 5, 7, 9 ]
12+
13+
);
14+
15+
for my $e (@examples) {
16+
my $list = join ',', $e->@*;
17+
my @out = missing_numbers( $e->@* );
18+
my $out = join ',', @out;
19+
say <<"END";
20+
Input: \@array = ($list)
21+
Output: $out
22+
END
23+
}
24+
25+
sub missing_numbers ( @array ) {
26+
my $n = scalar @array;
27+
my %h = map { $_ => 1 } @array;
28+
my @output;
29+
for my $v ( 0 .. $n ) {
30+
push @output, $v unless $h{$v};
31+
}
32+
return @output;
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use experimental qw{ say postderef signatures state };
6+
7+
use List::Util qw{ sum0 uniq };
8+
9+
my @examples = ( 1, 3, 5, 7 );
10+
11+
for my $e (@examples) {
12+
my @piles = penny_piles($e);
13+
@piles = sort { length $b <=> length $a }
14+
uniq sort map { join ',', $_->@* } @piles;
15+
my $count = scalar @piles;
16+
my $list = join "\n\t", '', @piles;
17+
say <<"END";
18+
Input: \$n = $e
19+
Output: $count
20+
$list
21+
END
22+
}
23+
24+
sub penny_piles ( $n, @array ) {
25+
my $sum = sum0 @array;
26+
my @copy = sort { $a <=> $b } @array;
27+
if ( $sum > $n ) { return }
28+
if ( $sum == $n ) {
29+
return \@copy;
30+
}
31+
32+
my @output;
33+
for my $i ( reverse 1 .. $n ) {
34+
my @local = sort { $a <=> $b } $n, @array;
35+
push @output, penny_piles( $n, @array, $i );
36+
}
37+
return @output;
38+
}
39+

0 commit comments

Comments
 (0)