aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-02 12:46:38 +0100
committerGitHub <noreply@github.com>2024-07-02 12:46:38 +0100
commita11e0933ddc1e2efd777a52120b2fb300a389d11 (patch)
tree1a95ac3418b3a225fb7ff9011a4b2d1c1a18f966
parent4070e3b4ff81c1b4f9dede46a6fa4eb26a1274ee (diff)
parent40473898c520fc50578648f5c8bb7424ee293fe6 (diff)
downloadperlweeklychallenge-club-a11e0933ddc1e2efd777a52120b2fb300a389d11.tar.gz
perlweeklychallenge-club-a11e0933ddc1e2efd777a52120b2fb300a389d11.tar.bz2
perlweeklychallenge-club-a11e0933ddc1e2efd777a52120b2fb300a389d11.zip
Merge pull request #10355 from pjcs00/wk276
Week 276 - Round days and frequent numbers
-rw-r--r--challenge-276/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-276/peter-campbell-smith/perl/ch-1.pl40
-rwxr-xr-xchallenge-276/peter-campbell-smith/perl/ch-2.pl37
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-276/peter-campbell-smith/blog.txt b/challenge-276/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..d0d7af0f73
--- /dev/null
+++ b/challenge-276/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/276
diff --git a/challenge-276/peter-campbell-smith/perl/ch-1.pl b/challenge-276/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..4656d3122e
--- /dev/null
+++ b/challenge-276/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-07-01
+use utf8; # Week 276 - task 1 - Complete day
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+my (@hours);
+
+complete_day(12, 12, 30, 24, 24);
+complete_day(72, 48, 24, 5);
+complete_day(12, 18, 24);
+
+push @hours, int(rand(30)) for 0 .. 24;
+complete_day(@hours);
+
+sub complete_day {
+
+ my (@hours, $pairs, $a, $b, $days, $explain);
+
+ @hours = @_;
+
+ # loop over all pairs
+ $pairs = 0;
+ $explain = '';
+ for $a (0 .. @hours - 2) {
+ for $b ($a + 1 .. @hours - 1) {
+
+ # determine if this pair sums to a whole number of days
+ $days = ($hours[$a] + $hours[$b]) / 24;
+ next unless (int($days) == $days);
+ $pairs ++;
+ $explain .= qq[($hours[$a] + $hours[$b]), ];
+ }
+ }
+ printf(qq[\nInput: \@hours = (%s)\n], join(', ', @hours));
+ printf(qq[Output: %s: %s\n], $pairs, substr($explain, 0, -2));
+}
diff --git a/challenge-276/peter-campbell-smith/perl/ch-2.pl b/challenge-276/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..c1ecd3fa62
--- /dev/null
+++ b/challenge-276/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-07-01
+use utf8; # Week 276 - task 2 - Maximum frequency
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+my @ints;
+
+maximum_frequency(1, 2, 2, 4, 1, 5);
+maximum_frequency(1, 2, 3, 4, 5);
+maximum_frequency(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2);
+
+push @ints, int(rand(15)) for 0 .. 99;
+maximum_frequency(@ints);
+
+sub maximum_frequency {
+
+ my (@ints, $max_freq, $int, %freq, $count, $explain, $s, $they);
+
+ @ints = @_;
+ $max_freq = 0;
+ for $int (@ints) {
+ $freq{$int} ++;
+ $max_freq = $freq{$int} if $freq{$int} > $max_freq;
+ }
+ for $int (sort keys %freq) {
+ next unless $freq{$int} == $max_freq;
+ $count += $freq{$int};
+ $explain .= qq[$int, ];
+ }
+
+ printf(qq[\nInput: \@ints = (%s)\n], join(', ', @ints));
+ printf(qq[Output: %d integers occur with frequency %d: %s\n], $count, $max_freq, substr($explain, 0, -2));
+}