aboutsummaryrefslogtreecommitdiff
path: root/challenge-208/lubos-kolouch/python/ch-2.py
blob: 38f78d4ba9a679b769e8cb0f40f0ce4db939a853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()