aboutsummaryrefslogtreecommitdiff
path: root/challenge-265/lubos-kolouch/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-265/lubos-kolouch/python')
-rw-r--r--challenge-265/lubos-kolouch/python/ch-1.py25
-rw-r--r--challenge-265/lubos-kolouch/python/ch-2.py20
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-265/lubos-kolouch/python/ch-1.py b/challenge-265/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..c059f9fa60
--- /dev/null
+++ b/challenge-265/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,25 @@
+from collections import Counter
+from typing import List, Optional
+
+
+def find_integer_appearing_33_percent_or_more(ints: List[int]) -> Optional[int]:
+
+ if not ints:
+ return None
+
+ count = Counter(ints)
+ n = len(ints)
+ threshold = n / 3
+
+ # Finding all numbers that appear at least 33% of the time
+ valid_numbers = [num for num, cnt in count.items() if cnt >= threshold]
+
+ # Return the smallest integer that meets the criteria, or None if no such integer exists
+ return min(valid_numbers) if valid_numbers else None
+
+
+# Test cases
+assert find_integer_appearing_33_percent_or_more([1, 2, 3, 3, 3, 3, 4, 2]) == 3
+assert find_integer_appearing_33_percent_or_more([1, 1]) == 1
+assert find_integer_appearing_33_percent_or_more([1, 2, 3]) == 1
+assert find_integer_appearing_33_percent_or_more([]) is None
diff --git a/challenge-265/lubos-kolouch/python/ch-2.py b/challenge-265/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..98270f0f99
--- /dev/null
+++ b/challenge-265/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,20 @@
+from collections import Counter
+from typing import List
+
+
+def shortest_completing_word(target: str, words: List[str]) -> str:
+ # Function to clean and count the letters in a word
+ def get_letter_counts(s: str) -> Counter:
+ return Counter(c.lower() for c in s if c.isalpha())
+
+ target_count = get_letter_counts(target)
+
+ # Filter and find the shortest completing word
+ valid_words = [word for word in words if not target_count - get_letter_counts(word)]
+ return min(valid_words, key=len) if valid_words else ""
+
+
+# Test cases
+assert shortest_completing_word("aBc 11c", ["accbbb", "abc", "abbc"]) == "accbbb"
+assert shortest_completing_word("Da2 abc", ["abcm", "baacd", "abaadc"]) == "baacd"
+assert shortest_completing_word("JB 007", ["jj", "bb", "bjb"]) == "bjb"