Robbie Hatley's Solutions To The Weekly Challenge #261

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

This week (2024-03-17 through 2024-03-23) is weekly challenge #261. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 261-1: Element Digit Sum
Submitted by Mohammad Sajid Anwar
Reworded for clarity by Robbie Hatley.
You are given an array of integers, @ints. Write a script to
evaluate the absolute value of the difference between the
element and digit sums of @ints.

Example 1:
Input: @ints = (1,2,3,45)
Output: 36
Element Sum: 1 + 2 + 3 + 45 = 51
Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
Absolute Difference: |51 - 15| = 36

Example 2:
Input: @ints = (1,12,3)
Output: 9
Element Sum: 1 + 12 + 3 = 16
Digit Sum: 1 + 1 + 2 + 3 = 7
Absolute Difference: |16 - 7| = 9

Example 3:
Input: @ints = (1,2,3,4)
Output: 0
Element Sum: 1 + 2 + 3 + 4 = 10
Digit Sum: 1 + 2 + 3 + 4 = 10
Absolute Difference: |10 - 10| = 0

Example 4:
Input: @ints = (236, 416, 336, 350)
Output: 1296

I'll use "sum0" from "List::Util" to sum number lists, "split" and "grep" to get the digits, and "abs" to get the absolute value of the difference between the element and digit sums:

   use v5.38;
   use utf8;
   use List::Util 'sum0';

   # Return the sum of the elements of any array of
   # decimal representations of real numbers:
   sub el_sum (@array) {
      return sum0 @array;
   }

   # Return the sum of the digits of any array of
   # decimal representations of real numbers:
   sub di_sum (@array) {
      return sum0 grep {$_ =~ /^[0-9]$/} map {split //, $_} @array;
   }

   # Return the absolute value of the difference between
   # the element sum and the digit sum of any array of
   # decimal representations of real numbers:
   sub abs_diff_el_di (@array) {
      return abs(el_sum(@array)-di_sum(@array));
   }

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 261-2: Multiply by Two
Submitted by: Mohammad Sajid Anwar
Reworded for clarity by Robbie Hatley
You are given an array of integers, @ints, and an integer, $start.
Write a script to do the followings:
1. Look for $start in the array @ints; if found, multiply the
   value of $start by 2 in-situ.
2. If not found, stop the process; otherwise, repeat.
3. Return the final value of $start.

Example 1:
Input: @ints = (5,3,6,1,12) and $start = 3
Output: 24
Step 1: 3 is in the array so 3 x 2 = 6
Step 2: 6 is in the array so 6 x 2 = 12
Step 3: 12 is in the array so 12 x 2 = 24
24 is not found in the array so return 24.

Example 2:
Input: @ints = (1,2,4,3) and $start = 1
Output: 8
Step 1: 1 is in the array so 1 x 2 = 2
Step 2: 2 is in the array so 2 x 2 = 4
Step 3: 4 is in the array so 4 x 2 = 8
8 is not found in the array so return 8.

Example 3:
Input: @ints = (5,6,7) and $start = 2
Output: 2
2 is not found in the array so return 2.

I'll use "any" from "List::Util" to determine whether $start is in @array, and "while" to repeatedly double $start for so long as $start is in @array:

   use v5.38;
   use utf8;
   use List::Util 'any';

   # Double $start while $start is in @array:
   sub mult_by_two ($start, @array) {
      return ($start *= 2 while any {$_ == $start} @array);
   }

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

That's it for challenge 261; see you on challenge 262!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239