-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreverse-vowels-string.py
More file actions
83 lines (66 loc) · 3.09 KB
/
reverse-vowels-string.py
File metadata and controls
83 lines (66 loc) · 3.09 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# https://leetcode.com/problems/reverse-vowels-of-a-string/
# Write a function that takes a string as input and reverse only the vowels of a string.
#
# Example 1:
# Given s = "hello", return "holle".
#
# Example 2:
# Given s = "leetcode", return "leotcede".
#
# Subscribe to see which companies asked this question
import unittest
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
if s is None:
return None
else:
vowels = ['a','e','i','o','u','A','E','I','O','U']
vowels_in_s = []
for i in xrange(len(s)):
if s[i] in vowels:
vowels_in_s.append(s[i])
else:
continue
s = list(s)
for i in xrange(len(s)):
if s[i] in vowels:
s[i] = vowels_in_s.pop()
else:
continue
return ''.join(s)
class TestReverseVowels(unittest.TestCase):
my_solution = Solution()
def test_simple_string(self):
self.assertEquals(self.my_solution.reverseVowels("hello"),"holle")
def test_capital_vowels(self):
self.assertEquals(self.my_solution.reverseVowels("aA"),"Aa")
def test_empty_string(self):
self.assertEquals(self.my_solution.reverseVowels(""),"")
def test_all_vowels_string(self):
self.assertEquals(self.my_solution.reverseVowels("aeiouAEIOU"),"UOIEAuoiea")
def test_no_vowels_string(self):
self.assertEquals(self.my_solution.reverseVowels("bcdfghjklmnpqrstvwxyz"),"bcdfghjklmnpqrstvwxyz")
def test_all_spaces_string(self):
self.assertEquals(self.my_solution.reverseVowels(" ")," ")
def test_no_vowels_with_leading_spaces_string(self):
self.assertEquals(self.my_solution.reverseVowels(" bcdfghjklmnpqrstvwxyz")," bcdfghjklmnpqrstvwxyz")
def test_no_vowels_with_trailing_spaces_string(self):
self.assertEquals(self.my_solution.reverseVowels("bcdfghjklmnpqrstvwxyz "),"bcdfghjklmnpqrstvwxyz ")
def test_no_vowels_with_leading_and_trailing_spaces_string(self):
self.assertEquals(self.my_solution.reverseVowels(" bcdfghjklmnpqrstvwxyz ")," bcdfghjklmnpqrstvwxyz ")
def test_all_vowels_with_leading_spaces(self):
self.assertEquals(self.my_solution.reverseVowels(" aeiouAEIOU")," UOIEAuoiea")
def test_all_vowels_with_trailing_spaces(self):
self.assertEquals(self.my_solution.reverseVowels("aeiouAEIOU "),"UOIEAuoiea ")
def test_all_vowels_with_leading_and_trailing_spaces(self):
self.assertEquals(self.my_solution.reverseVowels(" aeiouAEIOU ")," UOIEAuoiea ")
def test_no_vowel_string_alphanumeric(self):
self.assertEquals(self.my_solution.reverseVowels("123456789765432123456789"),"123456789765432123456789")
def test_no_vowels_special_characters(self):
self.assertEquals(self.my_solution.reverseVowels("!@#$%^&*(*&^%$#@#$%^%$#@"),"!@#$%^&*(*&^%$#@#$%^%$#@")
if __name__ == "__main__":
unittest.main()