diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-03-14 18:52:38 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-03-14 18:52:38 +0100 |
| commit | f8fccce87c49ef9d9e5da19d23e4c8549ff024c9 (patch) | |
| tree | 66611b8d21077569f77a5662a82402611d8358c2 /challenge-208/lubos-kolouch/python | |
| parent | 0f4a809c0bead5e3fd8bc4616e1c011c9d6fb8a9 (diff) | |
| download | perlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.tar.gz perlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.tar.bz2 perlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.zip | |
Challenge 208 LK Perl Python Bash
Diffstat (limited to 'challenge-208/lubos-kolouch/python')
| -rw-r--r-- | challenge-208/lubos-kolouch/python/ch-1.py | 58 | ||||
| -rw-r--r-- | challenge-208/lubos-kolouch/python/ch-2.py | 52 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-208/lubos-kolouch/python/ch-1.py b/challenge-208/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f29f86e8a7 --- /dev/null +++ b/challenge-208/lubos-kolouch/python/ch-1.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_common_strings(list1: List[str], list2: List[str]) -> List[str]: + """ + Finds all common strings in the given two arrays with minimum index sum. + If no common strings are found, returns an empty list. + Args: + list1: The first list of strings + list2: The second list of strings + Returns: + A list of common strings with minimum index sum, or an empty list if no common strings are found. + """ + common = {} + min_index_sum = len(list1) + len(list2) + for i, elem1 in enumerate(list1): + for j, elem2 in enumerate(list2): + if elem1 == elem2: + index_sum = i + j + if index_sum <= min_index_sum: + min_index_sum = index_sum + common[list1[i]] = index_sum + + result = [] + for key in common.keys(): + if common[key] == min_index_sum: + result.append(key) + + return result + + +# Tests + + +def test_find_common_strings(): + list1_1 = ["Perl", "Raku", "Love"] + list2_1 = ["Raku", "Perl", "Hate"] + common_1 = find_common_strings(list1_1, list2_1) + assert common_1 == ["Perl", "Raku"] + + list1_2 = ["A", "B", "C"] + list2_2 = ["D", "E", "F"] + common_2 = find_common_strings(list1_2, list2_2) + assert common_2 == [] + + list1_3 = ["A", "B", "C"] + list2_3 = ["C", "A", "B"] + common_3 = find_common_strings(list1_3, list2_3) + assert common_3 == ["A"] + + print("All tests pass") + + +if __name__ == "__main__": + test_find_common_strings() diff --git a/challenge-208/lubos-kolouch/python/ch-2.py b/challenge-208/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..38f78d4ba9 --- /dev/null +++ b/challenge-208/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List, Tuple, Union + + +def find_missing_and_duplicate(nums: List[int]) -> Union[Tuple[int, int], int]: + """ + Finds the duplicate and missing integer in a given sequence of integers. + + Args: + nums (List[int]): A list of integers with one missing and one duplicate. + + Returns: + Union[Tuple[int, int], int]: If both a missing and duplicate integer are found, returns a tuple + containing the duplicate integer followed by the missing integer. If none are found, returns -1. + + Example: + >>> find_missing_and_duplicate([1, 2, 2, 4]) + (2, 3) + >>> find_missing_and_duplicate([1, 2, 3, 4]) + -1 + >>> find_missing_and_duplicate([1, 2, 3, 3]) + (3, 4) + """ + count = {} + missing = 0 + duplicate = 0 + for num in nums: + count[num] = count.get(num, 0) + 1 + if count[num] > 1: + duplicate = num + for i in range(1, len(nums) + 2): + if i not in count: + missing = i + break + if missing and duplicate: + return (duplicate, missing) + else: + return -1 + + +# Run the tests + + +def test_find_missing_and_duplicate(): + assert find_missing_and_duplicate([1, 2, 2, 4]) == (2, 3) + assert find_missing_and_duplicate([1, 2, 3, 4]) == -1 + assert find_missing_and_duplicate([1, 2, 3, 3]) == (3, 4) + + +test_find_missing_and_duplicate() |
