|
| 1 | +VOWELS = set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']) |
| 2 | + |
| 3 | +def changeOfVowelsInCycle(cycle, text): |
| 4 | + rearranged = list(text)[::-1] |
| 5 | + vowelIndexes = [i for i, c in enumerate(rearranged) if c in VOWELS] |
| 6 | + |
| 7 | + numVowels = len(vowelIndexes) |
| 8 | + if numVowels > 1: |
| 9 | + |
| 10 | + i = 0 |
| 11 | + |
| 12 | + saved = [rearranged[vowelIndexes[j]] for j in xrange(cycle % numVowels)] |
| 13 | + while i < numVowels: |
| 14 | + vowelIndex = vowelIndexes[(i + cycle) % numVowels] |
| 15 | + saved.append(rearranged[vowelIndex]) |
| 16 | + rearranged[vowelIndex] = saved.pop(0) |
| 17 | + |
| 18 | + i += 1 |
| 19 | + |
| 20 | + return "".join(rearranged) |
| 21 | + |
| 22 | +def testChangeOfVowelsInCycle(): |
| 23 | + assert changeOfVowelsInCycle(1, "potato") == "ototap" |
| 24 | + assert changeOfVowelsInCycle(3, "this test is of potato") == "itetip fo sa tsot soht" |
| 25 | + assert changeOfVowelsInCycle(45, "aa bb cc dd ee ff gg hh ii jj") == "jj ea hh gg ff ai dd cc bb ie" |
| 26 | + assert changeOfVowelsInCycle(1, "abc") == "cba" |
| 27 | + assert changeOfVowelsInCycle(350, "a true magic is a potato think") == "knaht otatip i sa cegum airt o" |
| 28 | + assert changeOfVowelsInCycle(9, "CodeFights") == "sthgiFedoC" |
| 29 | + assert changeOfVowelsInCycle(10, "Ojf lsnelI UFlsn Eeiuo nnky Ynak jhA") == "ahj konY yknn uieEU nslFI elOnsl fjA" |
| 30 | + assert changeOfVowelsInCycle(28, "Once upon a time there lived three little foxes") == "sexif elttel eirht davol ureht Omet o nepi ecne" |
| 31 | + |
| 32 | +def main(): |
| 33 | + testChangeOfVowelsInCycle() |
| 34 | + |
| 35 | +if __name__ == "__main__": |
| 36 | + main() |
| 37 | + |
0 commit comments