diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-07-01 17:09:45 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-07-01 17:09:45 +0100 |
| commit | 40473898c520fc50578648f5c8bb7424ee293fe6 (patch) | |
| tree | d7f7c91bd01b78a34caf181be79c2512169cc56e | |
| parent | f18cb7a95e46b9ded70a2d1d932d0bb7b1772a67 (diff) | |
| download | perlweeklychallenge-club-40473898c520fc50578648f5c8bb7424ee293fe6.tar.gz perlweeklychallenge-club-40473898c520fc50578648f5c8bb7424ee293fe6.tar.bz2 perlweeklychallenge-club-40473898c520fc50578648f5c8bb7424ee293fe6.zip | |
Week 276 - Round days and frequent numbers
| -rw-r--r-- | challenge-276/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/peter-campbell-smith/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-276/peter-campbell-smith/perl/ch-2.pl | 37 |
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)); +} |
