aboutsummaryrefslogtreecommitdiff
path: root/challenge-276/ryan-thompson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-276/ryan-thompson/python')
-rwxr-xr-xchallenge-276/ryan-thompson/python/ch-1.py19
-rwxr-xr-xchallenge-276/ryan-thompson/python/ch-2.py29
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-276/ryan-thompson/python/ch-1.py b/challenge-276/ryan-thompson/python/ch-1.py
new file mode 100755
index 0000000000..9a217285c1
--- /dev/null
+++ b/challenge-276/ryan-thompson/python/ch-1.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+#
+# ch-1.py - Complete Day
+#
+# 2024 Ryan Thompson <rjt@cpan.org>
+
+def complete_day(hours):
+ count = 0
+ for i, m in enumerate(hours):
+ for n in filter(lambda n: ((m + n) % 24 == 0), hours[i+1:]):
+ count += 1
+
+ return(count)
+
+# Examples
+print(complete_day([12, 12, 30, 24, 24])) # 2
+print(complete_day([72, 48, 24, 5])) # 3
+print(complete_day([12, 18, 24])) # 0
+print(complete_day([])) # 0
diff --git a/challenge-276/ryan-thompson/python/ch-2.py b/challenge-276/ryan-thompson/python/ch-2.py
new file mode 100755
index 0000000000..08aff757c1
--- /dev/null
+++ b/challenge-276/ryan-thompson/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+#
+# ch-2.py - Maximum Frequency
+#
+# 2024 Ryan Thompson <rjt@cpan.org>
+
+# First identify which number(s) appear most often in the list,
+# then return the count of all such numbers. Ex. 1, 2, 2, 4, 1, 5
+# should return 4, because the matching numbers are 1,1,2,2 (freq:2)
+def max_freq(ints):
+ # Annoying special case for empty list
+ if len(ints) == 0:
+ return(0)
+
+ # Build the frequency table (freq[n] = # of times n is in ints)
+ freq = {}
+ for n in ints: freq[n] = freq.setdefault(n,0) + 1
+
+ max_freq = max(freq.values()) # Maximal frequency
+
+ return(sum(filter(lambda x: x == max_freq, freq.values())))
+
+# Examples
+print(max_freq([1, 2, 2, 4, 1, 5])) # 4
+print(max_freq([1, 2, 3, 4, 5])) # 5
+print(max_freq([1, 2, 2, 4, 6, 1, 5, 6])) # 6
+print(max_freq(['a', 'a', 'b', 'a', 'b', 'b', 'c'])) # 6
+print(max_freq([3.1415926])) # 1
+print(max_freq([])) # 0