Robbie Hatley's Solutions To The Weekly Challenge #276

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-06-30 through 2024-07-06 is #276. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 276-1: Complete Day
Submitted by: Mohammad Sajid Anwar
Given an array of integers, write a script to return the number
of pairs that forms a complete day. A complete day is defined as
a time duration that is an exact multiple of 24 hours.

Example 1 input:
[12, 12, 30, 24, 24],
Expected output: 2
Pair 1: (12, 12)
Pair 2: (24, 24)

Example 2 input:
[72, 48, 24, 5],
Expected output: 3
Pair 1: (72, 48)
Pair 2: (72, 24)
Pair 3: (48, 24)

Example 3 input:
[12, 18, 24],
Expected output: 0

This is just a matter of using nested 3-part loops to avoid duplicating pairs, then seeing which pairs add up to an integer x such that 0 == x%24.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 276-2: Maximum Frequency
Submitted by: Mohammad Sajid Anwar
Given an array of positive integers, write a script to return
the total number of elements in the given array which have the
highest frequency.

Example 1 input:
[1, 2, 2, 4, 1, 5],
Expected output: 4
The maximum frequency is 2.
The elements 1 and 2 has the maximum frequency.

Example 2 input:
[1, 2, 3, 4, 5],
Expected output: 5
The maximum frequency is 1.
The elements 1, 2, 3, 4 and 5 has the maximum frequency.

To solve this, I used "frequency" from "List::MoreUtils" to get a flat list of item->frequency pairs, then I used "pairs" from "List::Util" to convert the flat list into a list of pairs, then I used "sort {$b->[1]<=>$a->[1]}" to put the pairs in inverse order of frequency. I then tallied-up the frequencies of the items having maximum frequency.

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

That's it for challenge 276; see you on challenge 277!

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 #273