From d63377644b6fe7841a4e25eace5190494d2107cf Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 29 Jan 2024 21:47:46 +0000 Subject: add solutions week 254 in python --- challenge-254/steven-wilson/python/ch-02.py | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-254/steven-wilson/python/ch-02.py (limited to 'challenge-254/steven-wilson/python/ch-02.py') diff --git a/challenge-254/steven-wilson/python/ch-02.py b/challenge-254/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..3d2a7d6821 --- /dev/null +++ b/challenge-254/steven-wilson/python/ch-02.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + + +VOWELS = ['a', 'e', 'i', 'o', 'u'] + + +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() -- cgit From acd9de6a39c5dac057aafc2cf4351c06c7e9664e Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 30 Jan 2024 11:48:29 +0000 Subject: use set for membership test --- challenge-254/steven-wilson/python/ch-02.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-254/steven-wilson/python/ch-02.py') diff --git a/challenge-254/steven-wilson/python/ch-02.py b/challenge-254/steven-wilson/python/ch-02.py index 3d2a7d6821..9a9fc09f62 100644 --- a/challenge-254/steven-wilson/python/ch-02.py +++ b/challenge-254/steven-wilson/python/ch-02.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -VOWELS = ['a', 'e', 'i', 'o', 'u'] +VOWELS = set('aeiou') def reverse_vowels(string): -- cgit