Shortest Time
Task
You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
Write a script to find out the shortest time in minutes between any two time points.
Solution
|
|
Discussion
Having to return the time difference in minutes hints that it might be a good
idea to convert the time into minutes. The convert-to-minutes
function does
this using Raku’s powerful
Regexes and named captures. Time
differences of more than twelve hours are converted by subtracting them from 24
hours to get the smaller and desired time difference.
Having a function which calculates the difference for two times the rest is basic functional programming: mapping that function over all possible pairs and taking the minimum of the list.
Array Pairings
Task
You are given an array of integers having even number of elements..
Write a script to find the maximum sum of the minimum of each pairs.
Solution
|
|
Discussion
After sorting the given list split it after every second element. Each of the resulting pairs returns it’s first element. This can be done more efficiently by selecting all elements at even indices (starting at 0). Having the list of numbers simply summing them up to calculate the requested value.