File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
challenge-201/dave-jacoby/perl Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments