diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-20 18:53:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-20 18:53:31 +0100 |
| commit | 054d9fa00ed540ef760c5f2b089d74475861acfa (patch) | |
| tree | f7ef616ecc991d64e0254542b1406bd57a7c9d09 /challenge-265/lubos-kolouch/python | |
| parent | a35274e825f08cbd845c38e98a4dfc0686558861 (diff) | |
| parent | dc33da454b62dad3a43f4277b477892774ed82b5 (diff) | |
| download | perlweeklychallenge-club-054d9fa00ed540ef760c5f2b089d74475861acfa.tar.gz perlweeklychallenge-club-054d9fa00ed540ef760c5f2b089d74475861acfa.tar.bz2 perlweeklychallenge-club-054d9fa00ed540ef760c5f2b089d74475861acfa.zip | |
Merge pull request #9959 from LubosKolouch/master
Challenge 265 LK Perl Python
Diffstat (limited to 'challenge-265/lubos-kolouch/python')
| -rw-r--r-- | challenge-265/lubos-kolouch/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-265/lubos-kolouch/python/ch-2.py | 20 |
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" |
