diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-01 18:06:31 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-01 18:06:31 -0400 |
| commit | 12e47d464089a624eaa389ffcc3bc31f3d5a3823 (patch) | |
| tree | 9bd8db2935170d61bbbac2224df594736f5d7037 | |
| parent | f18cb7a95e46b9ded70a2d1d932d0bb7b1772a67 (diff) | |
| download | perlweeklychallenge-club-12e47d464089a624eaa389ffcc3bc31f3d5a3823.tar.gz perlweeklychallenge-club-12e47d464089a624eaa389ffcc3bc31f3d5a3823.tar.bz2 perlweeklychallenge-club-12e47d464089a624eaa389ffcc3bc31f3d5a3823.zip | |
new file: challenge-276/mattneleigh/perl/ch-1.pl
new file: challenge-276/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-276/mattneleigh/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-276/mattneleigh/perl/ch-2.pl | 72 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-276/mattneleigh/perl/ch-1.pl b/challenge-276/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..c83129b693 --- /dev/null +++ b/challenge-276/mattneleigh/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @hour_lists = ( + [ 12, 12, 30, 24, 24 ], + [ 72, 48, 24, 5 ], + [ 12, 18, 24 ] +); + +print("\n"); +foreach my $hour_list (@hour_lists){ + printf( + "Input: \@hours = (%s)\nOutput: %d\n\n", + join(", ", @{$hour_list}), + count_complete_day_pairs(@{$hour_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of integers that represent a length of time in whole hours from +# a common reference datum, determine how many pairs of these form a complete +# day- that is to say, there is an exact multiple of 24 between them; the +# integers need not be sorted +# Takes one argument: +# * A list of integers that represent a length of time in whole hours from a +# common reference datum (e.g. ( 72, 48, 24, 5 ) ) +# Returns: +# * The number of pairs of times that form a complete day (e.g. 3 ) +################################################################################ +sub count_complete_day_pairs{ + + my $day_pairs = 0; + + # Loop over every pair in the list + for my $i (0 .. $#ARG - 1){ + for my $j ($i + 1 .. $#ARG){ + # Increment the day pair counter if there is no + # remainder when dividing the difference between + # members of this pair by 24 + $day_pairs++ + unless(($ARG[$j] - $ARG[$i]) % 24); + } + } + + return($day_pairs); + +} + + + diff --git a/challenge-276/mattneleigh/perl/ch-2.pl b/challenge-276/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..7ef779693b --- /dev/null +++ b/challenge-276/mattneleigh/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 1, 2, 2, 4, 1, 5 ], + [ 1, 2, 3, 4, 5 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: %d\n\n", + join(", ", @{$integer_list}), + count_elements_at_max_frequency(@{$integer_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given an array of integers, count the number that occur at the highest +# frequency within the list; if two or more values occur at the maximum +# frequency, the total number of elements with those values will be returned +# Takes one argument: +# * A list of integers to examine (e.g. ( 1, 2, 2, 4, 1, 5 ) ) +# Returns: +# * The number of elements in the list that share occur at the highest +# frequency (e.g. 4, as both 1 and 2 are tied at the maximum frequency) +################################################################################ +sub count_elements_at_max_frequency{ + + my %freq_table; + my $max_freq = 0; + my $max_freq_total = 0; + + # Set up a table of frequencies keyed by + # values observed in the list- while also + # making note of the maximum frequency yet + # observed + foreach(@ARG){ + $freq_table{$_}++; + $max_freq = $freq_table{$_} + if($freq_table{$_} > $max_freq); + } + + # Calculate the total number of elements + # that had the maximum frequency (there may + # be more than one value that reached this + # count) + foreach(keys(%freq_table)){ + $max_freq_total += $max_freq + if($freq_table{$_} == $max_freq); + } + + return($max_freq_total); + +} + + + |
