I’m sorry for the lack of music this time around, but when I read “broken keys”, there’s only one broken key I can think of.
Let’s proceed with caution and approach Perl Weekly Challenge 275!
Continue readingI’m sorry for the lack of music this time around, but when I read “broken keys”, there’s only one broken key I can think of.
Let’s proceed with caution and approach Perl Weekly Challenge 275!
Continue readingI’m sorry for the strange musical free association. but … 🎶 thinking of a sweet romance beginning in a queue… 🎶 there’s a reason this song is not very far away in my programming-addled brain.
So let’s take a ride down to Perl Weekly Challenge 274!
Musical free association: “B after A” became “time to play B sides…“
This week’s challenge is all about characters: counting occurrences of a character in a string and returning what percentage of the string it is, and determining if one of two characters occurs in a string after the last occurrence of the other character.
Without further ado, Perl Weekly Challenge 273!
This week, we’re “defanging” IP addresses and calculating string scores, but both these tasks are easy enough we’ll be there in a minute.
Onward to Perl Weekly Challenge 272!
What with all the ones in today’s binary challenges, the first thing that popped into my head was James Taylor’s Only One.
I mean, who can blame me? Now that we’ve set the musical tone, let’s dive into Perl Weekly Challenge 271!
There’s antimony, arsenic, aluminum, selenium,
And hydrogen and oxygen and nitrogen and rhenium,
And nickel, neodymium, neptunium, germanium,
And iron, americium, ruthenium, uranium…
This week’s challenge is all about ELEMENTS! (That’s Perl Weekly Challenge 270, of course…)
Before I started on this tonight, I ran across a video of Antônio Carlos Jobim’s One Note Samba being performed by Dean Martin & Caterina Valente, and I knew I needed to make it the musical theme tonight, but I’m going to link you to John Pizzarelli’s version.
So, let’s samba on down to Perl Weekly Challenge 269!
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!
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
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.
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.
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.
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.
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)
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.
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.
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.
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