|
| 1 | +""" |
| 2 | +The task is to translate text from English to Pig Latin. |
| 3 | +The translation is defined using four rules, which look at the pattern of vowels |
| 4 | +and consonants at the beginning of a word. These rules look at each word's use |
| 5 | +of vowels and consonants: |
| 6 | +
|
| 7 | +vowels: the letters a, e, i, o, and u |
| 8 | +consonants: the other 21 letters of the English alphabet |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +def translate(text: str) -> str: |
| 13 | + """ |
| 14 | + Translate English text to Pig Latin. |
| 15 | +
|
| 16 | + :param text: The English text to translate |
| 17 | + :return: The text translated to Pig Latin |
| 18 | + """ |
| 19 | + words: list[str] = text.split(" ") |
| 20 | + return " ".join(process_words(word) for word in words) |
| 21 | + |
| 22 | + |
| 23 | +def process_words(text: str) -> str: |
| 24 | + """ |
| 25 | + Process a single word and convert it to Pig Latin using the four translation rules. |
| 26 | +
|
| 27 | + :param text: The English word to convert |
| 28 | + :return: The word converted to Pig Latin |
| 29 | + """ |
| 30 | + if not text: |
| 31 | + return "" |
| 32 | + |
| 33 | + # Rule 1 |
| 34 | + if is_vowel(text[0]) or text[:2] in ("xr", "yt"): |
| 35 | + # If a word begins with a vowel, |
| 36 | + # or starts with "xr" or "yt", |
| 37 | + # add an "ay" sound to the end of the word. |
| 38 | + return f"{text}ay" |
| 39 | + |
| 40 | + # Rule 2 |
| 41 | + if is_rule_2(text): |
| 42 | + # If a word begins with one or more consonants, first move those consonants |
| 43 | + # to the end of the word and then add an "ay" sound to the end of the word. |
| 44 | + i = get_consonant_cluster_length(text) |
| 45 | + return f"{text[i + 1:]}{text[: i + 1]}ay" |
| 46 | + |
| 47 | + # Rule 3 |
| 48 | + if is_rule_3(text): |
| 49 | + # If a word starts with zero or more consonants followed by "qu", first move |
| 50 | + # those consonants (if any) and the "qu" part to the end of the word, and then |
| 51 | + # add an "ay" sound to the end of the word. |
| 52 | + i = text.index("qu") |
| 53 | + return f"{text[i + 2:]}{text[: i + 2]}ay" |
| 54 | + |
| 55 | + # Rule 4 |
| 56 | + if is_rule_4(text): |
| 57 | + # If a word starts with one or more consonants followed by "y", first move the |
| 58 | + # consonants preceding the "y" to the end of the word, and then add an "ay" sound |
| 59 | + # to the end of the word. |
| 60 | + i = text.index("y") |
| 61 | + return f"{text[i:]}{text[:i]}ay" |
| 62 | + |
| 63 | + raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'") |
| 64 | + |
| 65 | + |
| 66 | +def is_rule_2(text: str) -> bool: |
| 67 | + """ |
| 68 | + Check ff a word begins with one or more consonants. |
| 69 | + No 'qu' or 'y' in it. |
| 70 | +
|
| 71 | + :param text: |
| 72 | + :return: |
| 73 | + """ |
| 74 | + return is_consonant(text[0]) and not is_rule_3(text) and not is_rule_4(text) |
| 75 | + |
| 76 | + |
| 77 | +def is_rule_3(text: str) -> bool: |
| 78 | + """ |
| 79 | + Check if a word starts with zero or more consonants followed by "qu". |
| 80 | +
|
| 81 | + :param text: |
| 82 | + :return: |
| 83 | + """ |
| 84 | + if "qu" in text: |
| 85 | + if text[:2] == "qu": |
| 86 | + return True |
| 87 | + |
| 88 | + for char in text[: text.index("qu")]: |
| 89 | + if is_vowel(char): |
| 90 | + return False |
| 91 | + return True |
| 92 | + return False |
| 93 | + |
| 94 | + |
| 95 | +def is_rule_4(text: str) -> bool: |
| 96 | + """ |
| 97 | + Check if a word starts with one or more consonants followed by "y". |
| 98 | +
|
| 99 | + :param text: |
| 100 | + :return: |
| 101 | + """ |
| 102 | + if "y" in text and text[0] != "y": |
| 103 | + for char in text[: text.index("y")]: |
| 104 | + if is_vowel(char): |
| 105 | + return False |
| 106 | + return True |
| 107 | + return False |
| 108 | + |
| 109 | + |
| 110 | +def is_vowel(char: str) -> bool: |
| 111 | + """ |
| 112 | + Check if a character is a vowel (a, e, i, o, or u). |
| 113 | +
|
| 114 | + :param char: The character to check |
| 115 | + :return: True if the character is a vowel, False otherwise |
| 116 | + """ |
| 117 | + return char in "aeiou" |
| 118 | + |
| 119 | + |
| 120 | +def is_consonant(char: str) -> bool: |
| 121 | + """ |
| 122 | + Check if a character is a consonant (one of the 21 non-vowel letters). |
| 123 | +
|
| 124 | + :param char: The character to check |
| 125 | + :return: True if the character is a consonant, False otherwise |
| 126 | + """ |
| 127 | + return char.isalpha() and not is_vowel(char) |
| 128 | + |
| 129 | + |
| 130 | +def get_consonant_cluster_length(text: str) -> int: |
| 131 | + """ |
| 132 | + Find the length of the consonant cluster at the beginning of a word. |
| 133 | +
|
| 134 | + :param text: The word to analyze |
| 135 | + :return: The index of the last consonant in the initial consonant cluster |
| 136 | + """ |
| 137 | + i = 0 |
| 138 | + for n, char in enumerate(text): |
| 139 | + if is_consonant(char): |
| 140 | + i = n |
| 141 | + else: |
| 142 | + break |
| 143 | + |
| 144 | + return i |
0 commit comments