diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-05-06 13:11:05 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-05-06 13:11:05 +0200 |
| commit | 294e99fbd23fcea14776af463de8da2d2b09e27d (patch) | |
| tree | 18d35e45af40cb3dfa5205b0ac8827ea57cd9afa | |
| parent | 479ca4e9c71aa2ea5be2bc7b66451c21be9d8dde (diff) | |
| download | perlweeklychallenge-club-294e99fbd23fcea14776af463de8da2d2b09e27d.tar.gz perlweeklychallenge-club-294e99fbd23fcea14776af463de8da2d2b09e27d.tar.bz2 perlweeklychallenge-club-294e99fbd23fcea14776af463de8da2d2b09e27d.zip | |
Add solution 268
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-268/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-268/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-268/jeanluc2020/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-268/jeanluc2020/perl/ch-2.pl | 59 |
4 files changed, 133 insertions, 0 deletions
diff --git a/challenge-268/jeanluc2020/blog-1.txt b/challenge-268/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..cd6580a42c --- /dev/null +++ b/challenge-268/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-268-1.html diff --git a/challenge-268/jeanluc2020/blog-2.txt b/challenge-268/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..f7f34508e9 --- /dev/null +++ b/challenge-268/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-268-2.html diff --git a/challenge-268/jeanluc2020/perl/ch-1.pl b/challenge-268/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..8124c69443 --- /dev/null +++ b/challenge-268/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/#TASK1 +# +# Task 1: Magic Number +# ==================== +# +# You are given two arrays of integers of same size, @x and @y. +# +# Write a script to find the magic number that when added to each elements of +# one of the array gives the second array. Elements order is not important. +# +# Example 1 +# +# Input: @x = (3, 7, 5) +# @y = (9, 5, 7) +# Output: 2 +# +# The magic number is 2. +# @x = (3, 7, 5) +# + 2 2 2 +# @y = (5, 9, 7) +# +# Example 2 +# +# Input: @x = (1, 2, 1) +# @y = (5, 4, 4) +# Output: 3 +# +# The magic number is 3. +# @x = (1, 2, 1) +# + 3 3 3 +# @y = (5, 4, 4) +# +# Example 3 +# +# Input: @x = (2) +# @y = (5) +# Output: 3 +# +############################################################ +## +## discussion +## +############################################################ +# +# By sorting both arrays, we have each element in each array paired up +# with the corresponding element of the other array, so the magic number +# is the difference from one element in one array to its corresponding +# element in the other array. We can even use this for a sanity check: +# if the elements don't match up at some point, then no magic number +# exists + +use strict; +use warnings; + +magic_number( [3, 7, 5], [9, 5, 7] ); +magic_number( [1, 2, 1], [5, 4, 4] ); +magic_number( [2], [5] ); + +sub magic_number { + my ($x, $y) = @_; + print "Input: (", join(", ", @$x), "), (", join(", ", @$y), ")\n"; + my @sorted_x = sort { $a <=> $b } @$x; + my @sorted_y = sort { $a <=> $b } @$y; + die "Both arrays don't have the same length!" unless scalar(@sorted_x) == scalar(@sorted_y); + my $magic = $sorted_y[0] - $sorted_x[0]; + foreach my $idx (0..$#sorted_x) { + die "There is no magic number that works for all elements of both arrays!" unless $sorted_x[$idx] + $magic == $sorted_y[$idx]; + } + print "Output: $magic\n"; +} + diff --git a/challenge-268/jeanluc2020/perl/ch-2.pl b/challenge-268/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..29d66a697a --- /dev/null +++ b/challenge-268/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/#TASK2 +# +# Task 2: Number Game +# =================== +# +# You are given an array of integers, @ints, with even number of elements. +# +# Write a script to create a new array made up of elements of the given array. +# Pick the two smallest integers and add it to new array in decreasing order +# i.e. high to low. Keep doing until the given array is empty. +# +## Example 1 +## +## Input: @ints = (2, 5, 3, 4) +## Output: (3, 2, 5, 4) +## +## Round 1: we picked (2, 3) and push it to the new array (3, 2) +## Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) +# +## Example 2 +## +## Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +## Output: (1, 1, 4, 3, 6, 4, 9, 6) +# +## Example 3 +## +## Input: @ints = (1, 2, 2, 3) +## Output: (2, 1, 3, 2) +# +############################################################ +## +## discussion +## +############################################################ +# +# First, we sort the input array. Then we pick the first two elements +# in reverse order until there are no more elements in the array. + +use strict; +use warnings; + +number_game(2, 5, 3, 4); +number_game(9, 4, 1, 3, 6, 4, 6, 1); +number_game(1, 2, 2, 3); + +sub number_game { + my @ints = @_; + print "Input: (", join(", ", @ints), ")\n"; + my @sorted = sort { $a <=> $b } @ints; + my @result = (); + while(scalar(@sorted) >= 2) { + my $first = shift @sorted; + my $second = shift @sorted; + push @result, ($second, $first); + } + print "Output: (", join(", ", @result), ")\n"; +} + |
