aboutsummaryrefslogtreecommitdiff
path: root/challenge-254/steven-wilson/python/ch-02.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-31 13:17:47 +0000
committerGitHub <noreply@github.com>2024-01-31 13:17:47 +0000
commitc05e33d4361f0c9fbd112bea0bc69ea9e2e442f3 (patch)
tree8fb8d0a40405f1a862ed32749f3b9ca89276bbb0 /challenge-254/steven-wilson/python/ch-02.py
parent309292106bdcdf741608474b63f5fed9615ddf73 (diff)
parentacd9de6a39c5dac057aafc2cf4351c06c7e9664e (diff)
downloadperlweeklychallenge-club-c05e33d4361f0c9fbd112bea0bc69ea9e2e442f3.tar.gz
perlweeklychallenge-club-c05e33d4361f0c9fbd112bea0bc69ea9e2e442f3.tar.bz2
perlweeklychallenge-club-c05e33d4361f0c9fbd112bea0bc69ea9e2e442f3.zip
Merge pull request #9490 from oWnOIzRi/week254
add solutions week 254 in python
Diffstat (limited to 'challenge-254/steven-wilson/python/ch-02.py')
-rw-r--r--challenge-254/steven-wilson/python/ch-02.py40
1 files changed, 40 insertions, 0 deletions
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..9a9fc09f62
--- /dev/null
+++ b/challenge-254/steven-wilson/python/ch-02.py
@@ -0,0 +1,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()