Skip to content

Commit 0cf3fb1

Browse files
author
Albert Hu
authored
Merge pull request #19 from alberthu16/day25
Day 25: change of vowels in cycle
2 parents 4637d0e + 05f722d commit 0cf3fb1

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

day25/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
question of the day: https://codefights.com/challenge/CA2ShAfLkb5D5JHCG
2+
3+
Given a text, reverse its characters and shift all of the vowels in a cycle.
4+
5+
The vowels are 'a', 'e', 'i', 'o', and 'u' and can be upper- and lowercase.
6+
7+
Example
8+
9+
For cycle = 1 and text = "potato", the output should be
10+
changeOfVowelsInCycle(cycle, text) = "ototap".
11+
12+
Reversed, the text becomes "otatop". The vowels in the text are 'o', 'a' and 'o' (in this exact order). When shifted by one, they become 'o', 'o' and 'a', so the final answer is "ototap".
13+
14+
## Ideas
15+
16+
I needa do two things: 1) reverse the string, 2) do the cycly thingy with the vowels.
17+
The cycly thingy is the slightly tricky part. However, I can just think of the
18+
vowels as a kind of circularly linked list where I can forget about parts of the list
19+
when I'm done with them. I can use a queue to hold the vowels that are in the process
20+
of being swapped into a new position. And I'll pop from this queue as I go through
21+
the indices of vowels that I need to swap.
22+
23+
part 1 is `O(n)` runtime, and requires `O(n)` space because I create a list using the
24+
characters of the string before I do the reversal.
25+
26+
part 2 is `O(n)` runtime as well because in the worst case, all the input characters
27+
are vowels.
28+
29+
So overall `O(n)` runtime, `O(n)` space. Is there a better solution? Maybe.
30+
31+
## Code
32+
33+
[Python](./changeOfVowelsInCycle.py)
34+
35+
## Follow up
36+

day25/changeOfVowelsInCycle.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)