This week’s theme is feuding in my head. On the musical side, “Number Game” made me think of Joni Mitchell, but the repeated use of the word “number” in the task titles made me think of Marketplace Radio (yes, I’m a Public Radio geek).
Anyway, let’s do Perl Weekly Challenge 268!
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
Approach
Element order may not be important in specifying the problem, but it feels pretty important in solving the problem. Since we’re looking for a number that, when added to each element of the first array yields an element of the second array, the obvious solution is to sort each array in either ascending or descending order, and then subtract the element in the first array from its corresponding element in the second array. As long as we get the same number each time, we’ve found the magic number. None of the examples show two input arrays that don’t yield a magic number, but nothing in the problem description precludes that.
Raku
sub magicNumber(@x, @y) {
my @xS = @x.sort;
my @yS = @y.sort;
my $magic = @yS.shift - @xS.shift;
while (@xS) {
if (@yS.shift - @xS.shift != $magic) {
return; # no magic number
}
}
return $magic;
}
$ raku/ch-1.raku
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 = (4, 5, 4)
Example 3:
Input: @x = (2)
@y = (5)
Output: 3
The magic number is 3.
@x = (2)
+ 3
@y = (5)
Example 4:
Input: @x = (1, 2)
@y = (4, 2)
Output: no magic number
View the entire Raku script for this task on GitHub.
Perl
sub magicNumber($x, $y) {
my @xS = sort @$x;
my @yS = sort @$y;
my $magic = shift(@yS) - shift(@xS);
while (@xS) {
if (shift(@yS) - shift(@xS) != $magic) {
return; # no magic number
}
}
return $magic;
}
View the entire Perl script for this task on GitHub.
Python
def magicNumber(x, y):
xS = sorted(x)
yS = sorted(y)
magic = yS.pop(0) - xS.pop(0)
while xS:
if yS.pop(0) - xS.pop(0) != magic:
return None; # no magic number
return magic
View the entire Python script for this task on GitHub.
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)
Approach
This feels very much like the previous task: we need to sort the elements so we can pick the two smallest integers, we pull those values off the sorted array (only one array this time, however), and we do some kind of comparison. The big difference this time is we’re adding the elements back to a list.
Raku
The big thing to note here is that Raku’s Array push doesn’t flatten it’s argument list, so “If you pass an array or list as the thing to push, it becomes one additional element; multiple values are added to the array only if you supply them as separate arguments or in a slip.”
sub numberGame(@ints) {
my @intSorted = @ints.sort;
my @new;
while (@intSorted) {
my $x = @intSorted.shift;
my $y = @intSorted.shift;
if ($x > $y) {
@new.push: ($x, $y).Slip;
}
else {
@new.push: ($y, $x).Slip;
}
}
return @new;
}
$ raku/ch-2.raku
Example 1:
Input: @ints = (2, 5, 3, 4)
Output: (3, 2, 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)
View the entire Raku script for this task on GitHub.
Perl
sub numberGame(@ints) {
my @intSorted = sort @ints;
my @new;
while (@intSorted) {
my $x = shift @intSorted;
my $y = shift @intSorted;
if ($x > $y) {
push @new, $x, $y;
}
else {
push @new, $y, $x;
}
}
return @new;
}
View the entire Perl script for this task on GitHub.
Python
def numberGame(ints):
intSorted = sorted(ints)
new = []
while intSorted:
x = intSorted.pop(0)
y = intSorted.pop(0)
if x > y:
new.extend([x, y])
else:
new.extend([y, x])
return new
View the entire Python script for this task on GitHub.
Here’s all my solutions in GItHub: https://github.com/packy/perlweeklychallenge-club/tree/master/challenge-268/packy-anderson