diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-13 19:31:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-13 19:31:50 +0100 |
| commit | 5c483b3ceb526bc01c18b320279a281c2ce3b0da (patch) | |
| tree | c286a39c69eed7384cc5e912e53add2ed3e1ae6b /challenge-277 | |
| parent | e4eccedffc6755bc1c21fd53ca8229338cf0d72e (diff) | |
| parent | dd99f0b84214e3233ce6e1d8bbed8524144551a9 (diff) | |
| download | perlweeklychallenge-club-5c483b3ceb526bc01c18b320279a281c2ce3b0da.tar.gz perlweeklychallenge-club-5c483b3ceb526bc01c18b320279a281c2ce3b0da.tar.bz2 perlweeklychallenge-club-5c483b3ceb526bc01c18b320279a281c2ce3b0da.zip | |
Merge pull request #10419 from jeanluc2020/jeanluc-277
Add solution 277.
Diffstat (limited to 'challenge-277')
| -rw-r--r-- | challenge-277/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-277/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-277/jeanluc2020/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-277/jeanluc2020/perl/ch-2.pl | 69 |
4 files changed, 140 insertions, 0 deletions
diff --git a/challenge-277/jeanluc2020/blog-1.txt b/challenge-277/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..38c796603b --- /dev/null +++ b/challenge-277/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-277-1.html diff --git a/challenge-277/jeanluc2020/blog-2.txt b/challenge-277/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..05ee8bad18 --- /dev/null +++ b/challenge-277/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-277-2.html diff --git a/challenge-277/jeanluc2020/perl/ch-1.pl b/challenge-277/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..e3ddccdcc0 --- /dev/null +++ b/challenge-277/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/#TASK1 +# +# Task 1: Count Common +# ==================== +# +# You are given two array of strings, @words1 and @words2. +# +# Write a script to return the count of words that appears in both arrays +# exactly once. +# +## Example 1 +## +## Input: @words1 = ("Perl", "is", "my", "friend") +## @words2 = ("Perl", "and", "Raku", "are", "friend") +## Output: 2 +## +## The words "Perl" and "friend" appear once in each array. +# +## Example 2 +## +## Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar") +## @words2 = ("Python", "is", "top", "in", "guest", "languages") +## Output: 1 +# +## Example 3 +## +## Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional") +## @words2 = ("Crystal", "is", "similar", "to", "Ruby") +## Output: 0 +# +############################################################ +## +## discussion +## +############################################################ +# +# For each word in both arrays, count their number of occurrences. +# In the end, for each word, add 1 to the output of it occurs in both +# arrays exactly once. + +use strict; +use warnings; + +count_common( ["Perl", "is", "my", "friend"], ["Perl", "and", "Raku", "are", "friend"] ); +count_common( ["Perl", "and", "Python", "are", "very", "similar"], ["Python", "is", "top", "in", "guest", "languages"] ); +count_common( ["Perl", "is", "imperative", "Lisp", "is", "functional"], ["Crystal", "is", "similar", "to", "Ruby"] ); + +sub count_common { + my $words1 = shift; + my $words2 = shift; + my $all_words = {}; + print "Input: (", join(", ", @$words1), "), (", join(", ", @$words2), ")\n"; + # count words in first array + foreach my $word (@$words1) { + $all_words->{$word}->{"1"}++; + } + # count words in second array + foreach my $word (@$words2) { + $all_words->{$word}->{"2"}++; + } + my $output = 0; + foreach my $word (keys %$all_words) { + next unless $all_words->{$word}->{"1"}; + next unless $all_words->{$word}->{"2"}; + $output++ if $all_words->{$word}->{"1"} == 1 && $all_words->{$word}->{"2"} == 1; + } + print "Output: $output\n"; +} diff --git a/challenge-277/jeanluc2020/perl/ch-2.pl b/challenge-277/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..e2a009f59e --- /dev/null +++ b/challenge-277/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/#TASK2 +# +# Task 2: Strong Pair +# =================== +# +# You are given an array of integers, @ints. +# +# Write a script to return the count of all strong pairs in the given array. +# +### A pair of integers x and y is called strong pair if it satisfies: +### 0 < |x - y| < min(x, y). +# +## Example 1 +## +## Input: @ints = (1, 2, 3, 4, 5) +## Ouput: 4 +## +## Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) +# +## Example 2 +## +## Input: @ints = (5, 7, 1, 7) +## Ouput: 1 +## +## Strong Pairs: (5, 7) +# +############################################################ +## +## discussion +## +############################################################ +# +# We just walk the array from beginning to end for x and from +# the current position to the end for y, then check if the two +# numbers at those positions satisfy the condition for being a +# strong pair. However, we only count the strong pair if it hasn't +# occured earlier, as can be seen in example 2 (the task itself +# is unclear about this). It is also unclear whether strong pairs +# (a, b) and (b, a) are considered the same; so in my solution I +# consider those the same. +# + +use strict; +use warnings; +use List::Util qw(min max); + +strong_pair(1, 2, 3, 4, 5); +strong_pair(5, 7, 1, 7); + +sub strong_pair { + my @ints = @_; + print "Input: (", join(", ", @ints), ")\n"; + my $seen = {}; + my $output = 0; + foreach my $i (0..$#ints) { + foreach my $j ($i+1..$#ints) { + my $abs = abs($ints[$i] - $ints[$j]); + my $min = min($ints[$i], $ints[$j]); + my $max = max($ints[$i], $ints[$j]); + next if $seen->{$min}->{$max}; + next if $abs == 0; + next if $abs >= $min; + $seen->{$min}->{$max} = 1; + $output++; + } + } + print "Output: $output\n"; +} |
