aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-08-20 16:12:32 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-08-20 16:12:32 -0400
commitb605f7f4bbc3e912e0023bf03c323858d035067b (patch)
tree26ab25ee71d3255be5d5a7405a63c09f2774fbe7
parentf9ccc7e49e7cf61ab9159d49ba4d4f8249cda78e (diff)
downloadperlweeklychallenge-club-b605f7f4bbc3e912e0023bf03c323858d035067b.tar.gz
perlweeklychallenge-club-b605f7f4bbc3e912e0023bf03c323858d035067b.tar.bz2
perlweeklychallenge-club-b605f7f4bbc3e912e0023bf03c323858d035067b.zip
DAJ 283
-rw-r--r--challenge-283/dave-jacoby/perl/ch-1.pl33
-rw-r--r--challenge-283/dave-jacoby/perl/ch-2.pl31
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;
+}