diff options
| -rw-r--r-- | challenge-283/dave-jacoby/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-283/dave-jacoby/perl/ch-2.pl | 31 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-283/dave-jacoby/perl/ch-1.pl b/challenge-283/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..1b1dc7820a --- /dev/null +++ b/challenge-283/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ postderef say signatures state }; + +use List::Util qw{ uniq }; + +my @examples = ( # added a couple test entries + + [ 3, 3, 1 ], + [ 3, 2, 4, 2, 4 ], + [1], + [ 4, 3, 1, 1, 1, 4 ] +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = unique_number($example); + say <<"END"; + Input: \@int = ($input) + Output: $output +END +} + +sub unique_number ($input) { + my $hash; + for my $i ( @$input ) { + $hash->{$i}++; + } + my @results = grep {$hash->{$_} == 1 } keys %$hash; + return shift @results; +} diff --git a/challenge-283/dave-jacoby/perl/ch-2.pl b/challenge-283/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..65aceb597a --- /dev/null +++ b/challenge-283/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures }; + +my @examples = ( # added a couple test entries + + [ 1, 2, 1, 0 ], + [ 0, 3, 0 ], +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = digital_count_value($example) + ? 'true' : 'false'; + say <<"END"; + Input: \@int = ($input) + Output: $output +END +} + +sub digital_count_value ($input) { + my $s = -1 + scalar $input->@*; + for my $i ( 0 .. $s ) { + my $j = $input->[$i]; + my $k = scalar grep { $_ == $i } $input->@*; + return 0 unless $j == $k; + } + return 1; +} |
