diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-21 00:15:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-21 00:15:57 +0100 |
| commit | 9cb1c77ad86b3c04ee0dda74946d7a3649dc4733 (patch) | |
| tree | 1c1f3d82f5c8b6365fd9a4eca8431a390ac87ddd | |
| parent | f867370a8caddeda9a4ddf42bb01ebd4758c8c91 (diff) | |
| parent | b605f7f4bbc3e912e0023bf03c323858d035067b (diff) | |
| download | perlweeklychallenge-club-9cb1c77ad86b3c04ee0dda74946d7a3649dc4733.tar.gz perlweeklychallenge-club-9cb1c77ad86b3c04ee0dda74946d7a3649dc4733.tar.bz2 perlweeklychallenge-club-9cb1c77ad86b3c04ee0dda74946d7a3649dc4733.zip | |
Merge pull request #10675 from jacoby/master
DAJ 283
| -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; +} |
