diff options
| -rw-r--r-- | challenge-276/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-276/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/jeanluc2020/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-276/jeanluc2020/perl/ch-2.pl | 62 |
4 files changed, 126 insertions, 0 deletions
diff --git a/challenge-276/jeanluc2020/blog-1.txt b/challenge-276/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..b581b617fe --- /dev/null +++ b/challenge-276/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-276-1.html diff --git a/challenge-276/jeanluc2020/blog-2.txt b/challenge-276/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..1d948b4245 --- /dev/null +++ b/challenge-276/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-276-2.html diff --git a/challenge-276/jeanluc2020/perl/ch-1.pl b/challenge-276/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..a743c2b2b1 --- /dev/null +++ b/challenge-276/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-276/#TASK1 +# +# Task 1: Complete Day +# ==================== +# +# You are given an array of integers, @hours. +# +# Write a script to return the number of pairs that forms a complete day. +# +### A complete day is defined as a time duration that is an exact multiple of +### 24 hours. +# +## Example 1 +## +## Input: @hours = (12, 12, 30, 24, 24) +## Output: 2 +## +## Pair 1: (12, 12) +## Pair 2: (24, 24) +# +## Example 2 +## +## Input: @hours = (72, 48, 24, 5) +## Output: 3 +## +## Pair 1: (72, 48) +## Pair 2: (72, 24) +## Pair 3: (48, 24) +# +## Example 3 +## +## Input: @hours = (12, 18, 24) +## Output: 0 +# +############################################################ +## +## discussion +## +############################################################ +# +# Create all possible pairs and check which ones are multiples +# of 24. + +use strict; +use warnings; + +complete_day(12, 12, 30, 24, 24); +complete_day(72, 48, 24, 5); +complete_day(12, 18, 24); + +sub complete_day { + my @hours = @_; + print "Input: (", join(", ", @hours), ")\n"; + my $output = 0; + foreach my $i (0..$#hours) { + foreach my $j ($i+1..$#hours) { + $output++ unless (($hours[$i]+$hours[$j]) % 24); + } + } + print "Output: $output\n"; +} diff --git a/challenge-276/jeanluc2020/perl/ch-2.pl b/challenge-276/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..464242b3e7 --- /dev/null +++ b/challenge-276/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-276/#TASK2 +# +# Task 2: Maximum Frequency +# ========================= +# +# You are given an array of positive integers, @ints. +# +# Write a script to return the total number of elements in the given array +# which have the highest frequency. +# +## Example 1 +## +## Input: @ints = (1, 2, 2, 4, 1, 5) +## Ouput: 4 +## +## The maximum frequency is 2. +## The elements 1 and 2 has the maximum frequency. +# +## Example 2 +## +## Input: @ints = (1, 2, 3, 4, 5) +## Ouput: 5 +## +## The maximum frequency is 1. +## The elements 1, 2, 3, 4 and 5 has the maximum frequency. +# +############################################################ +## +## discsussion +## +############################################################ +# +# First, we calculate the frequencies for all numbers in the array. +# Then we sort that list by frequency and count all that share the +# maximum value. + +use strict; +use warnings; + +maximum_frequency(1, 2, 2, 4, 1, 5); +maximum_frequency(1, 2, 3, 4, 5); + +sub maximum_frequency { + my @ints = @_; + my $freq = {}; + print "Input: (", join(", ", @ints), ")\n"; + foreach my $i (@ints) { + $freq->{$i}++; + } + my $max = 0; + my $output = 0; + foreach my $i (sort { $freq->{$b} <=> $freq->{$a}} keys %$freq) { + if($max < $freq->{$i}) { + $max = $freq->{$i}; + } + if($freq->{$i} == $max) { + $output+=$max; + } + } + print "Output: $output\n"; +} |
