diff options
Diffstat (limited to 'challenge-187/mohammad-anwar/python')
| -rw-r--r-- | challenge-187/mohammad-anwar/python/ch-1.py | 65 | ||||
| -rw-r--r-- | challenge-187/mohammad-anwar/python/ch-2.py | 61 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-187/mohammad-anwar/python/ch-1.py b/challenge-187/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..7af49bbaad --- /dev/null +++ b/challenge-187/mohammad-anwar/python/ch-1.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 + +''' + +Week 187: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-187 + +Task #1: Days Together + + Two friends, Foo and Bar gone on holidays seperately 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. + +''' + +import unittest +from datetime import date, timedelta + +def _date(dateStr) -> date: + (day, month) = dateStr.split("-") + return date(2022, int(month), int(day)) + +def _days_together(fromDate, toDate, _toDate) -> int: + if toDate > _toDate: + toDate = _toDate + + delta = toDate - fromDate + return delta.days + 1 + +def daysTogether(sd1, ed1, sd2, ed2) -> int: + _sd1 = _date(sd1) + _ed1 = _date(ed1) + _sd2 = _date(sd2) + _ed2 = _date(ed2) + + days = 0 + + if _ed1 < _sd2: + return days + + if _sd2 <= _sd1: + days = _days_together(_sd1, _ed1, _ed2) + elif _sd2 <= _ed2: + days = _days_together(_sd2, _ed1, _ed2) + + return days + +# +# +# Unit test class + +class TestDaysTogether(unittest.TestCase): + def test_daysTogether(self): + self.assertEqual(daysTogether('12-01','20-01','15-01','18-01'), 4) + self.assertEqual(daysTogether('02-03','12-03','13-03','14-03'), 0) + self.assertEqual(daysTogether('02-03','12-03','11-03','15-03'), 2) + self.assertEqual(daysTogether('30-03','05-04','28-03','02-04'), 4) + +unittest.main() diff --git a/challenge-187/mohammad-anwar/python/ch-2.py b/challenge-187/mohammad-anwar/python/ch-2.py new file mode 100644 index 0000000000..7aefb10fe3 --- /dev/null +++ b/challenge-187/mohammad-anwar/python/ch-2.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +''' + +Week 187: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-187 + +Task #2: Magical Triplets + + 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. + +''' + +import unittest +import itertools + +def magicalTriplets(array): + array.sort() + magical = {} + for triplet in itertools.permutations(array, 3): + triplet_list = list(triplet) + triplet_list.sort(reverse=True) + a = triplet_list[0] + b = triplet_list[1] + c = triplet_list[2] + if (a + b > c) and (b + c > a) and (a + c > b): + key = ':'.join([str(i) for i in triplet_list]) + val = sum(triplet_list) + magical[key] = val + + if len(magical) == 0: + return [] + + magical_triplet = sorted(magical, key=magical.get, reverse=True)[0] + return [int(i) for i in magical_triplet.split(":")] + +# +# +# Unit test class + +class TestMagicalTriplets(unittest.TestCase): + def test_MagicalTriplets(self): + self.assertEqual(magicalTriplets([1, 2, 3, 2]), [3, 2, 2], 'Example 1') + self.assertEqual(magicalTriplets([1, 3, 2]), [], 'Example 2') + self.assertEqual(magicalTriplets([1, 1, 2, 3]), [], 'Example 3') + self.assertEqual(magicalTriplets([2, 4, 3]), [4, 3, 2], 'Example 4') + +unittest.main() |
