aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-19 13:24:23 +0100
committerGitHub <noreply@github.com>2024-08-19 13:24:23 +0100
commitddb3183f3f759718bad8892908f93bb8dc84de59 (patch)
tree4f1313c095c56f8885587c682e686e2293658cfe
parent08cfa9bfcf173dd023f951c76ec0ac0ec2c81dde (diff)
parent721fc9fe0db86c13438d51bcc52220ace48822cc (diff)
downloadperlweeklychallenge-club-ddb3183f3f759718bad8892908f93bb8dc84de59.tar.gz
perlweeklychallenge-club-ddb3183f3f759718bad8892908f93bb8dc84de59.tar.bz2
perlweeklychallenge-club-ddb3183f3f759718bad8892908f93bb8dc84de59.zip
Merge pull request #10656 from pjcs00/wk283
Week 283 - Occurrences
-rw-r--r--challenge-283/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-283/peter-campbell-smith/perl/ch-1.pl36
-rwxr-xr-xchallenge-283/peter-campbell-smith/perl/ch-2.pl38
3 files changed, 75 insertions, 0 deletions
diff --git a/challenge-283/peter-campbell-smith/blog.txt b/challenge-283/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..4ba3b47ce9
--- /dev/null
+++ b/challenge-283/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/283
diff --git a/challenge-283/peter-campbell-smith/perl/ch-1.pl b/challenge-283/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..e9b2249434
--- /dev/null
+++ b/challenge-283/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-08-19
+use utf8; # Week 283 - task 1 - Unique number
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+unique_number(1, 1, 2, 3, 4, 5, 3, 4, 5);
+unique_number(3, 2, 4, 2, 4);
+unique_number(7);
+unique_number(4, 3, 1, 1, 4);
+unique_number(123, 456, 77, 9999, 55, 55, 9999, 77, 456);
+
+sub unique_number {
+
+ my (@ints, %freq, $j);
+
+ @ints = @_;
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . ')';
+
+ # count occurrences of each number
+ $freq{$_} ++ for @ints;
+
+ # find the (first) one that only occurs once
+ for $j (keys %freq) {
+ if ($freq{$j} == 1) {
+ say qq[Output: $j];
+ return;
+ }
+ }
+
+ # none occurs once
+ say qq[Output: none];
+}
diff --git a/challenge-283/peter-campbell-smith/perl/ch-2.pl b/challenge-283/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..3d471143f7
--- /dev/null
+++ b/challenge-283/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-08-19
+use utf8; # Week 283 - task 2 - Digit count value
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+digit_count_value(1, 2, 1, 0);
+digit_count_value(0, 3, 0);
+digit_count_value(1, 2, 3, 2, 1, 0);
+
+sub digit_count_value {
+
+ my (@ints, %freq, $i, $result);
+
+ # calculate frequency of each occurring element
+ @ints = @_;
+ $freq{$_} ++ for @ints;
+
+ # test the challenge assertion
+ $result = 'true';
+ for $i (0 .. @ints - 1) {
+
+ # zero is allowed
+ $freq{$i} = 0 unless defined $freq{$i};
+
+ # quit unless assertion is true
+ if ($ints[$i] != $freq{$i}) {
+ $result = qq[false ∵ \$ints[$i] = $ints[$i] and $i occurs $freq{$i} times];
+ last;
+ }
+ }
+
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . ')';
+ say qq[Output: $result];
+}