I'm having trouble with the Pig Latin exercise

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

“rhythm” fits into the category where the word starts with non-vowels and a “y”.

Also, if 'qu' in text: – “liquid”.

well from the test cases rhythm doesn’t change:

AssertionError: 'rhythm' != 'ythmrhay'
- rhythm
+ ythmrhay

and to be honest i’m very confused on how to deal with whole phrases as well

What does this line do?

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:

  1. beginning with qu
  2. beginning with qu and a preceding consonant
  3. y is treated like a vowel at the end of a consonant cluster
  4. y as second letter in 2 letter word
  5. a whole phrase

@Lord8Bits If you’re looking for a bit of a deep dive on how to think about the structure of this exercise, this video might help you:

2 Likes