I’m trying to figure out why the word ‘rhythm’ is not working, and I can’t think of a solution for the whole phrase test.
import re
CONSONANTS = 'yqwrtpsdfghjklzxcvbnm'
def translate(text):
if re.match(r'^[aeiou]|xr|yt', text): #Checks if the word starts with a vowel or xr or yt
return text + 'ay'
if 'qu' in text:
qu_index = text.find('qu')
return text[qu_index + 2:] + text[:qu_index + 2] + 'ay'
if 'y' in text:
y_index = text.find('y')
if 0 < y_index <= 1:
return text[y_index:] + text[:y_index] + 'ay'
for i, char in enumerate(text):
if char not in CONSONANTS:
return text[i:] + text[:i] + 'ay'
return text
to be honest i was just testing and forgot to undo those tests but here’s my new updated code:
import re
CONSONANTS = 'yqwrtpsdfghjklzxcvbnm'
def translate(text):
qu_index = text.find('qu')
y_index = text.find('y')
def is_qu_y_before(s):
for char in 'aeiou':
vowel_index = text.find(char)
if vowel_index != -1:
if (qu_index != -1 and qu_index < vowel_index) or (y_index != -1 and y_index < vowel_index):
return True
return False
return True
if re.match(r'^[aeiou]|xr|yt', text):
return text + 'ay'
if 'qu' in text and 'y' in text and is_qu_y_before(text):
if qu_index < y_index:
return text[qu_index+2:] + text[:qu_index+2] + 'ay'
return text[y_index:] + text[:y_index] + 'ay'
for i, char in enumerate(text):
if char not in CONSONANTS:
return text[i:] + text[:i] + 'ay'
return text
for now there are 5 test cases in which it doesn’t work on:
beginning with qu
beginning with qu and a preceding consonant
y is treated like a vowel at the end of a consonant cluster