From 4f69a51e87ac0e32786d81e99457db6bd5fac53b Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 1 Jul 2024 12:22:05 +0100 Subject: add solutions week 276 in python --- challenge-276/steven-wilson/python/ch-1.py | 26 ++++++++++++++++++++++++++ challenge-276/steven-wilson/python/ch-2.py | 23 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 challenge-276/steven-wilson/python/ch-1.py create mode 100644 challenge-276/steven-wilson/python/ch-2.py (limited to 'challenge-276') diff --git a/challenge-276/steven-wilson/python/ch-1.py b/challenge-276/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..29fcb845e3 --- /dev/null +++ b/challenge-276/steven-wilson/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +from itertools import combinations + + +def complete_day(*hours): + ''' Given an array of integers (hours),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. + + >>> complete_day(12, 12, 30, 24, 24) + 2 + >>> complete_day(72, 48, 24, 5) + 3 + >>> complete_day(12, 18, 24) + 0 + ''' + return sum(1 for pair in combinations(hours, 2) if sum(pair) % 24 == 0) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-276/steven-wilson/python/ch-2.py b/challenge-276/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..7735ae8328 --- /dev/null +++ b/challenge-276/steven-wilson/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def maximum_frequency(*integers): + ''' Given an array of positive integers, return the total number of + elements in the given array which have the highest frequency. + + >>> maximum_frequency(1, 2, 2, 4, 1, 5) + 4 + >>> maximum_frequency(1, 2, 3, 4, 5) + 5 + ''' + counter = Counter(integers) + frequency = list(zip(*counter.most_common()))[1] + return sum(value for value in frequency if value == frequency[0]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From 72960a5959a03cb493e6c83a7ee32bc0a919c17d Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 1 Jul 2024 13:29:09 +0100 Subject: change variable names --- challenge-276/steven-wilson/python/ch-2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-276') diff --git a/challenge-276/steven-wilson/python/ch-2.py b/challenge-276/steven-wilson/python/ch-2.py index 7735ae8328..bf6a6edb55 100644 --- a/challenge-276/steven-wilson/python/ch-2.py +++ b/challenge-276/steven-wilson/python/ch-2.py @@ -13,8 +13,8 @@ def maximum_frequency(*integers): 5 ''' counter = Counter(integers) - frequency = list(zip(*counter.most_common()))[1] - return sum(value for value in frequency if value == frequency[0]) + frequencies = list(zip(*counter.most_common()))[1] + return sum(f for f in frequencies if f == frequencies[0]) if __name__ == "__main__": -- cgit