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


VOWELS = set('aeiou')


def reverse_vowels(string):
    ''' Given a string reverse all the vowels (a, e, i, o, u) and return
    resulting string.

    >>> reverse_vowels('Raku')
    'Ruka'
    >>> reverse_vowels('Perl')
    'Perl'
    >>> reverse_vowels('Julia')
    'Jaliu'
    >>> reverse_vowels('Uiua')
    'Auiu'
    '''
    string = string.lower()
    indexes = []
    cs = []
    for index, c in enumerate(string):
        if c in VOWELS:
            indexes.append(index)
            cs.append(c)

    if len(indexes) < 2:
        return string.title()
    else:
        characters = list(string)
        for index in indexes:
            characters[index] = cs.pop()
        return ''.join(characters).title()


if __name__ == "__main__":
    import doctest

    doctest.testmod()