Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #343 (“Zero Friend” and “Champion Team”)

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

The Weekly Challenge for the week of 2025-10-13 through 2025-10-19 is #343. The tasks for challenge #343 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 343-1: Zero Friend
Submitted by: Mohammad Sajid Anwar
You are given a list of numbers. Find the number that is closest
to zero and return its distance to zero.

Example #1:  Input: (4, 2, -1, 3, -2)      Output: 1

Example #2:  Input: (-5, 5, -3, 3, -1, 1)  Output: 1

Example #3:  Input: (7, -3, 0, 2, -8)      Output: 0

Example #4:  Input: (-2, -5, -1, -8)       Output: 1

Example #5:  Input: (-2, 2, -4, 4, -1, 1)  Output: 1

I store an ascending numeric sort of the absolute values of the given numbers in an array, then return the 0th element of that array.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 343-2: Champion Team
Submitted by: Mohammad Sajid Anwar
You have n teams in a tournament. A matrix grid tells you which
team is stronger between any two teams:
If grid[i][j] == 1, then team i is stronger than team j
If grid[i][j] == 0, then team j is stronger than team i
Find the champion team - the one with most wins, or if there is
no single such team, the strongest of the teams with most wins.
(You may assume that there is a definite answer.)

Example #1:
Input:           [0, 1, 1],
                 [0, 0, 1],
                 [0, 0, 0],
Expected output: Team 0

Example #2:
Input:           [0, 1, 0, 0],
                 [0, 0, 0, 0],
                 [1, 1, 0, 0],
                 [1, 1, 1, 0],
Expected output: Team 3

Example #3:
Input:           [0, 1, 0, 1],
                 [0, 0, 1, 1],
                 [1, 0, 0, 0],
                 [0, 0, 1, 0],
Expected output: Team 0

Example #4:
Input:           [0, 1, 1],
                 [0, 0, 0],
                 [0, 1, 0],
Expected output: Team 0

Example #5:
Input:           [0, 0, 0, 0, 0],
                 [1, 0, 0, 0, 0],
                 [1, 1, 0, 1, 1],
                 [1, 1, 0, 0, 0],
                 [1, 1, 0, 1, 0],
Expected output: Team 2

Note: Even though the description says "You may assume that there is a definite answer.", that's just not so. For example, there may be three teams with maximum number of wins each, with A beating B, B beating C, and C beating A. That would be a tie. So as tie-breaker, I'll give rapidly-increasing bonuses for beating higher- ranking teams. I'll start my making an array @w of wins. Then I'll make an array @s of scores with each team's score being its number of wins plus a bonus for each team beaten consisting of one billionth times w**w where w is the number-of-wins of the beaten team. For example, if a team beats one rank 2 team, one rank 3 team, and one rank 4 team, then its score will be (3 + 1E-9*2**2 + 1E-9*3**3 + 1E-9*4**4). Finally, I'll return the index of the first team found with max score. (Thus team order is ultimate tie breaker.)

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

That's it for challenge 343; see you on challenge 344!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)