Robbie Hatley's Solutions To The Weekly Challenge #290 (With A Touch Of Macbeth)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2024-10-06 through 2024-10-12 is #290.

Because these tasks both involve doubling things, my theme for this week is the witches' song from William Shakespeare's play Macbeth, Act IV, scene 1:

Double, double toil and trouble;
Fire burn and caldron bubble.
Fillet of a fenny snake,
In the caldron boil and bake;
Eye of newt and toe of frog,
Wool of bat and tongue of dog,
Adder's fork and blind-worm's sting,
Lizard's leg and howlet's wing,
For a charm of powerful trouble,
Like a hell-broth boil and bubble.

The tasks for challenge #290 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 290-1: "Double Exists"
Submitted by: Mohammad Sajid Anwar

You are given an array of integers, @ints. Write a script to
find if there exist two indices $i and $j such that:
1) $i != $j
2) 0 <= ($i, $j) < scalar @ints
3) $ints[$i] == 2 * $ints[$j]

Example 1:
Input: @ints = (6, 2, 3, 3)
Output: true
For $i = 0, $j = 2
$ints[$i] = 6 => 2 * 3 =>  2 * $ints[$j]

Example 2:
Input: @ints = (3, 1, 4, 13)
Output: false

Example 3:
Input: @ints = (2, 1, 4, 2)
Output: true
For $i = 2, $j = 3
$ints[$i] = 4 => 2 * 2 =>  2 * $ints[$j]

A pair of nested three-part loops will solve this easily.

Robbie Hatley's Perl Solution to The Weekly Challenge 290-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 290-2: "Luhn’s Algorithm"
Submitted by: Andrezgz

You are given a string $str containing digits (and possibly
other characters which can be ignored). The last digit is the
payload; consider it separately. Counting from the right, double
the value of the first, third, etc. of the remaining digits.
For each value now greater than 9, sum its digits. The correct
check digit is that which, added to the sum of all values, would
bring the total mod 10 to zero. Return true if and only if the
payload is equal to the correct check digit.

It was originally posted on reddit.

Example 1:
Input: "17893729974"
Output: true
Payload is 4.
Digits from the right:
7 * 2 = 14, sum = 5
9 = 9
9 * 2 = 18, sum = 9
2 = 2
7 * 2 = 14, sum = 5
3 = 3
9 * 2 = 18, sum = 9
8 = 8
7 * 2 = 14, sum = 5
1 = 1
Sum of all values = 56, so 4 must be added to bring the total
mod 10 to zero. The payload is indeed 4.

Example 2:
Input: "4137 8947 1175 5904"
Output: true

Example 3:
Input: "4137 8974 1175 5904"
Output: false

I actually found this one easier than task 1. It's basically just a matter of following the steps of Luhn's Algorithm as defined in the problem description:

  1. Get the "payload" (right-most digit).
  2. From the right, double the digit at every 0-based even index (2,4,6...); if digit sum > 9, add the digits.
  3. The "checksum" is the number necessary to be added to sum-of-digits to bring it up to a factor of 10.
  4. Return true if-and-only-if payload == checksum; else return false.

Robbie Hatley's Perl Solution to The Weekly Challenge 290-2

That's it for challenge 290; see you on challenge 291!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #276