diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-02 12:50:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-02 12:50:33 +0100 |
| commit | 67da1592fd006180e034e28747c8e76ca75361cc (patch) | |
| tree | 92982f005f131bf5f7cc038f0211ac4bc3075f8b | |
| parent | 19f14cae53014f5dd00bff6b54b31eba4f89fadf (diff) | |
| parent | 890142337dfa2da9840675b0ef134b8dd09cc3af (diff) | |
| download | perlweeklychallenge-club-67da1592fd006180e034e28747c8e76ca75361cc.tar.gz perlweeklychallenge-club-67da1592fd006180e034e28747c8e76ca75361cc.tar.bz2 perlweeklychallenge-club-67da1592fd006180e034e28747c8e76ca75361cc.zip | |
Merge pull request #10359 from rjt-pl/master
rjt's Week 276 solutions and blog
| -rw-r--r-- | challenge-276/ryan-thompson/README.md | 10 | ||||
| -rw-r--r-- | challenge-276/ryan-thompson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/ryan-thompson/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-276/ryan-thompson/perl/ch-2.pl | 34 | ||||
| -rwxr-xr-x | challenge-276/ryan-thompson/python/ch-1.py | 19 | ||||
| -rwxr-xr-x | challenge-276/ryan-thompson/python/ch-2.py | 29 |
6 files changed, 124 insertions, 4 deletions
diff --git a/challenge-276/ryan-thompson/README.md b/challenge-276/ryan-thompson/README.md index 7a4e388b49..588de6d8b1 100644 --- a/challenge-276/ryan-thompson/README.md +++ b/challenge-276/ryan-thompson/README.md @@ -1,15 +1,17 @@ # Ryan Thompson -## Week 259 Solutions +## Week 276 Solutions -### Task 1 › Banking Day Offset +### Task 1 › Complete Day * [Perl](perl/ch-1.pl) + * [Python](python/ch-1.py) -### Task 2 › Line Parser +### Task 2 › Maximum Frequency * [Perl](perl/ch-2.pl) + * [Python](python/ch-2.py) ## Blog - * [Bank Holidays and Line Parser](https://ry.ca/2024/03/pwc-259-bank-holidays-and-line-parser/) + * [Maximum Frequency and now my Day is Complete](https://ry.ca/2024/07/pwc-276-complete-day-and-maximum-frequency/) diff --git a/challenge-276/ryan-thompson/blog.txt b/challenge-276/ryan-thompson/blog.txt new file mode 100644 index 0000000000..8e89370e93 --- /dev/null +++ b/challenge-276/ryan-thompson/blog.txt @@ -0,0 +1 @@ +https://ry.ca/2024/07/pwc-276-complete-day-and-maximum-frequency/ diff --git a/challenge-276/ryan-thompson/perl/ch-1.pl b/challenge-276/ryan-thompson/perl/ch-1.pl new file mode 100755 index 0000000000..2bfd2b651e --- /dev/null +++ b/challenge-276/ryan-thompson/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +# +# ch-1.pl - Complete Day +# +# 2024 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +no warnings 'uninitialized'; +use List::Util qw< sum0 reduce>; + +# If any pair of numbers is a multiple of 24, return the count of +# such matching pairs. Assumption: pairs are WITH replacement. +sub complete_day { + my $count = 0; + while (my $m = shift) { + $count += sum0 map { ($m + $_) % 24 == 0 } @_ + } + $count +} + +sub complete_day_functional { + sum0 map { my $m = shift @$_; map { ($m + $_) % 24 == 0 } @$_ } + map { [ @_[$_ .. $#_] ] } 0..$#_ +} + +use Test::More; + +is complete_day(qw<12 12 30 24 24>), 2 => 'Example 1'; +is complete_day(qw<72 48 24 5>), 3 => 'Example 2'; +is complete_day(qw<12 18 24>), 0 => 'Example 3'; +is complete_day(), 0 => 'Empty list'; + +done_testing; diff --git a/challenge-276/ryan-thompson/perl/ch-2.pl b/challenge-276/ryan-thompson/perl/ch-2.pl new file mode 100755 index 0000000000..e854457625 --- /dev/null +++ b/challenge-276/ryan-thompson/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +# +# ch-2.pl - Maximum Frequency +# +# 2024 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +no warnings 'uninitialized'; +use List::Util qw< sum0 max >; + +# First identify which number(s) appear most often in the list, +# then return the count of all such numbers. Ex. 1, 2, 2, 4, 1, 5 +# should return 4, because the matching numbers are 1,1,2,2 (freq:2) +sub max_freq { + my %freq; # Frequency table + $freq{$_}++ for @_; + + my $max_freq = max values %freq; # Maximal frequency + + $max_freq * grep { $_ == $max_freq } values %freq; +} + +use Test::More; + +is max_freq(qw< 1 2 2 4 1 5 >), 4 => 'Example 1'; +is max_freq(qw< 1 2 3 4 5 >), 5 => 'Example 2'; +is max_freq(qw< 1 2 2 4 6 1 5 6 >), 6 => 'Example 3'; +is max_freq(qw< a a b a b b c >), 6 => 'Non-numeric'; +is max_freq(3.1415926), 1 => 'Single'; +is max_freq(), 0 => 'Empty'; + +done_testing; diff --git a/challenge-276/ryan-thompson/python/ch-1.py b/challenge-276/ryan-thompson/python/ch-1.py new file mode 100755 index 0000000000..9a217285c1 --- /dev/null +++ b/challenge-276/ryan-thompson/python/ch-1.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# +# ch-1.py - Complete Day +# +# 2024 Ryan Thompson <rjt@cpan.org> + +def complete_day(hours): + count = 0 + for i, m in enumerate(hours): + for n in filter(lambda n: ((m + n) % 24 == 0), hours[i+1:]): + count += 1 + + return(count) + +# Examples +print(complete_day([12, 12, 30, 24, 24])) # 2 +print(complete_day([72, 48, 24, 5])) # 3 +print(complete_day([12, 18, 24])) # 0 +print(complete_day([])) # 0 diff --git a/challenge-276/ryan-thompson/python/ch-2.py b/challenge-276/ryan-thompson/python/ch-2.py new file mode 100755 index 0000000000..08aff757c1 --- /dev/null +++ b/challenge-276/ryan-thompson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# ch-2.py - Maximum Frequency +# +# 2024 Ryan Thompson <rjt@cpan.org> + +# First identify which number(s) appear most often in the list, +# then return the count of all such numbers. Ex. 1, 2, 2, 4, 1, 5 +# should return 4, because the matching numbers are 1,1,2,2 (freq:2) +def max_freq(ints): + # Annoying special case for empty list + if len(ints) == 0: + return(0) + + # Build the frequency table (freq[n] = # of times n is in ints) + freq = {} + for n in ints: freq[n] = freq.setdefault(n,0) + 1 + + max_freq = max(freq.values()) # Maximal frequency + + return(sum(filter(lambda x: x == max_freq, freq.values()))) + +# Examples +print(max_freq([1, 2, 2, 4, 1, 5])) # 4 +print(max_freq([1, 2, 3, 4, 5])) # 5 +print(max_freq([1, 2, 2, 4, 6, 1, 5, 6])) # 6 +print(max_freq(['a', 'a', 'b', 'a', 'b', 'b', 'c'])) # 6 +print(max_freq([3.1415926])) # 1 +print(max_freq([])) # 0 |
