| 1 | 1 | def translate(text: str) -> str: |
| 2 | | pass |
| 2 | if not text: |
| 3 | return "" |
| 4 | |
| 5 | words = text.split() |
| 6 | translated_words = [] |
| 7 | |
| 8 | vowels = set('aeiou') |
| 9 | |
| 10 | for word in words: |
| 11 | # Edge Case: Empty word |
| 12 | if not word: |
| 13 | translated_words.append("") |
| 14 | continue |
| 15 | |
| 16 | # Rule 1: Word starts with vowel, "xr", or "yt" |
| 17 | if word[0] in vowels or word.startswith("xr") or word.startswith("yt"): |
| 18 | translated_words.append(word + "ay") |
| 19 | continue |
| 20 | |
| 21 | # Rule 3: Check for consonant(s) + "qu" pattern at the beginning |
| 22 | qu_index = word.find("qu") |
| 23 | if qu_index > 0 and qu_index < 3: # "qu" found early in word (after 1-2 consonants) |
| 24 | # Check if everything before "qu" are consonants |
| 25 | prefix = word[:qu_index] |
| 26 | if all(c not in vowels for c in prefix): |
| 27 | # Move consonants before "qu" and the "qu" to the end |
| 28 | translated_words.append(word[qu_index + 2:] + word[:qu_index + 2] + "ay") |
| 29 | continue |
| 30 | |
| 31 | # Rule 4: Check for consonant(s) + "y" pattern (y acts as vowel) |
| 32 | y_index = word.find("y") |
| 33 | if y_index > 0: # "y" found and not at start |
| 34 | # Move consonants before "y" to the end, "y" stays at beginning |
| 35 | translated_words.append(word[y_index:] + word[:y_index] + "ay") |
| 36 | continue |
| 37 | |
| 38 | # Rule 2: Word starts with consonant(s) |
| 39 | # Find first vowel, but treat "qu" as a consonant unit |
| 40 | first_vowel_index = 0 |
| 41 | while first_vowel_index < len(word): |
| 42 | # Check for "qu" as a unit |
| 43 | if first_vowel_index < len(word) - 1 and word[first_vowel_index] == 'q' and word[first_vowel_index + 1] == 'u': |
| 44 | first_vowel_index += 2 |
| 45 | break |
| 46 | # Check for regular vowel |
| 47 | elif word[first_vowel_index] in vowels: |
| 48 | break |
| 49 | else: |
| 50 | first_vowel_index += 1 |
| 51 | |
| 52 | if first_vowel_index == 0: # No vowels found at start (shouldn't happen due to Rule 1) |
| 53 | translated_words.append(word + "ay") |
| 54 | elif first_vowel_index >= len(word): # All consonants or ends with "qu" |
| 55 | translated_words.append(word + "ay") |
| 56 | else: |
| 57 | translated_words.append(word[first_vowel_index:] + word[:first_vowel_index] + "ay") |
| 58 | |
| 59 | return " ".join(translated_words) |
| 60 | |
| 61 | # Handled Edge Cases: empty string, words starting with "xr" or "yt", words starting with vowels, words with "qu" sequence, words with "y" after consonants, words consisting only of consonants |