DEV Community

Simon Green
Simon Green

Posted on

Magical Days Together with Triplets

Weekly Challenge 187

Challenge, My solution

After a few interstate trips and a bout of COVID-19, it's now onward and upwards to the end of the year. The ICC Men's Twenty20 World Cup has also started here so it's time to change the channel and throw out the remote for the next month. Go Black Caps!

Task 1: Days Together

Task

Two friends, Foo and Bar gone on holidays seperately[sic] to the same city. You are given their schedule i.e. start date and end date.

To keep the task simple, the date is in the form DD-MM and all dates belong to the same calendar year i.e. between 01-01 and 31-12. Also the year is non-leap year and both dates are inclusive.

Write a script to find out for the given schedule, how many days they spent together in the city, if at all.

My solution

I really like this challenge, as it makes you think about how to solve them. Date math is never easy. For this task, I convert the four input dates into day of the year (1 = January 1st, 365 = December 31st). In Python, I use the date module, while I use Date::Calc for the Perl solution. This makes the rest of the solution relatively easy.

I check that both Foo and Bar don't leave before they arrive. We can calculate the number of days by calculating when the last person arrives and when the first person leaves. If this is a negative number, they didn't see each other. Others we take the value and add 1 to print the solution.

Examples

$ ./ch-1.py 12-01 20-01 15-01 18-01
4 days

$ ./ch-1.py 02-03 12-03 13-03 14-03
0 days

$ ./ch-1.py 02-03 12-03 11-03 14-03
2 days

$ ./ch-1.py 30-03 05-04 28-03 02-04
4 days
Enter fullscreen mode Exit fullscreen mode

Task 2: Magical Triplets

Task

You are given a list of positive numbers, @n, having at least 3 numbers.

Write a script to find the triplets (a, b, c) from the given list that satisfies the following rules.

  1. a + b > c
  2. b + c > a
  3. a + c > b
  4. a + b + c is maximum.

In case, you end up with more than one triplets having the maximum then pick the triplet where a >= b >= c.

My solution

I'm sure there are smarter people doing this challenge that will come up with some formula for figuring out the quickest way to solve this, but some times the brute force approach is good enough. Especially when dealing with small number of things.

For this challenge, I use the combinations function from itertools in Python and from Algorithm::Combinatorics in Perl to compute all possible combinations of three digits.

I store the 'best' solution so far in the solutions list (which is set to None initially in Python, and an empty arrayref in Perl).

I then loop through each combination and determine if the first three criteria are met. If they and the solution value is empty or the sum is greater than the current solution, I set solution to the new list.

I finally print the result in the specified format.

Examples

$ ./ch-2.py 1 2 3 2
(3, 2, 2)

$ ./ch-2.py 1 3 2
()

$ ./ch-2.py 1 1 2 3
()

$ ./ch-2.py 2 4 3
(4, 3, 2)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)