aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/steven-wilson/python/ch-1.py
blob: dac2d858abf33b600bd60575b057027a9fbe374d (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
#!/usr/bin/env python3

from collections import Counter


def odd_character(original, shuffled):
    ''' Given two strings, the second string generated using the shuffled
    characters of the first string with an additional character.  Function
    returns the additional character.
    >>> odd_character("Perl", "Preel")
    'e'
    >>> odd_character("Weekly", "Weeakly")
    'a'
    >>> odd_character("Box", "Boxy")
    'y'
    '''
    if not original or not shuffled:
        raise ValueError("Both strings must be non-empty.")

    original_counter = Counter(x.casefold() for x in original)
    shuffled_counter = Counter(x.casefold() for x in shuffled)

    difference_counter = shuffled_counter - original_counter

    if not difference_counter or sum(difference_counter.values()) > 1:
        raise ValueError("No unique additional character found.")

    return next(difference_counter.elements())


if __name__ == "__main__":
    import doctest

    doctest.testmod()