## Weekly Challenge 276

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

## Task 1: Complete Day

### Task

You are given an array of integers, `@hours`

.

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.

### My solution

For this task, I could have used the combinations generator to create the pairs, but that seems like overkill for this task.

Instead I use a double loop. The other loop - with the variable `i`

- is from 0 to two less than the number of items in the list. The inner loop - with the variable `j`

- is from one more than `i`

to one less than the number of items in the list. This method ensures that we use every possible pairs.

I have a variable called `count`

which records the number of pairs when the combination of hours is a multiple of 24.

```
def complete_day(hours: list) -> int:
count = 0
items = len(hours)
for i in range(items-1):
for j in range(i+1, items):
if (hours[i] + hours[j]) % 24 == 0:
count += 1
return count
```

### Examples

```
$ ./ch-1.py 12 12 30 24 24
2
$ ./ch-1.py 72 48 24 5
3
$ ./ch-1.py 12 18 24
0
```

## Task 2: Maximum Frequency

### Task

You are given an array of positive integers, `@ints`

.

Write a script to return the total number of elements in the given array which have the highest frequency.

### My solution

For this task, I use the Counters function to turn the the list into a dict of frequencies. The key is the integer, the value is the number of times it occurs. Perl doesn't have a similar function, so I do this manually in my Perl solution.

The steps I take is as follows:

- Calculate the frequency of each integer, and store this in the
`freq`

dict (hash in Perl). - Find the maximum frequency, and store this as
`max_freq`

. - Count the number of elements in the
`freq`

dict that have`max_freq`

. This is stored as the`elements`

variable. - Return the product of the
`max_freq`

and`elements`

variable. This represents the number of items in the original array that have the highest frequency.

```
def maximum_frequency(ints: list) -> str:
freq = Counter(ints)
max_freq = max(freq.values())
elements = sum(1 for v in freq.values() if v == max_freq)
return elements * max_freq
```

### Examples

```
$ ./ch-2.py 1 2 2 4 1 5
4
$ ./ch-2.py 1 2 3 4 5
5
```

## Top comments (0)