Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #320 (“Maximum Count” and “Sum Difference”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of taks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-05-05 through 2025-05-11 is #320

The tasks for challenge #320 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 320-1: Maximum Count
Submitted by: Mohammad Sajid Anwar
You are given an array of integers. Write a script to return the
maximum between the number of positive and negative integers.
Zero is neither positive nor negative.

Example #1:
Input: @ints = (-3, -2, -1, 1, 2, 3)
Output: 3
There are 3 positive integers.
There are 3 negative integers.
The maximum between 3 and 3 is 3.

Example #2:
Input: @ints = (-2, -1, 0, 0, 1)
Output: 2
There are 1 positive integers.
There are 2 negative integers.
The maximum between 2 and 1 is 2.

Example #3:
Input: @ints = (1, 2, 3, 4)
Output: 4
There are 4 positive integers.
There are 0 negative integers.
The maximum between 4 and 0 is 4.

To solve this problem, I'll grep for negatives, and grep for positives, and return the max between the scalars of those two lists.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 320-2: Sum Difference
Submitted by: Mohammad Sajid Anwar
You are given an array of positive integers. Write a script to
return the absolute difference between digit sum and element sum
of the given array.

Example #1:
Input: @ints = (1, 23, 4, 5)
Output: 18
Element sum: 1 + 23 + 4 + 5 => 33
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 33 - 15 | => 18

Example #2:
Input: @ints = (1, 2, 3, 4, 5)
Output: 0
Element sum: 1 + 2 + 3 + 4 + 5 => 15
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 15 - 15 | => 0

Example #3:
Input: @ints = (1, 2, 34)
Output: 27
Element sum: 1 + 2 + 34 => 37
Digit sum: 1 + 2 + 3 + 4 => 10
Absolute difference: | 37 - 10 | => 27

To solve this problem, I'll split-and-sum the digits to get the digit sum, then sum the ints, then return the absolute value of the difference between those two sums.

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

That's it for challenge 320; see you on challenge 321!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #319 (“Word Count” and “Minimum Common”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #318 (“Group Positions” and “Reverse Equals”)