|
| 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 text from English to Pig Latin. |
| 15 | +
|
| 16 | + :param text: |
| 17 | + :return: |
| 18 | + """ |
| 19 | + words: list = text.split(' ') |
| 20 | + return " ".join(process_text(word) for word in words) |
| 21 | + |
| 22 | + |
| 23 | +def process_text(text: str) -> str: |
| 24 | + # Rule 1 |
| 25 | + # If a word begins with a vowel, |
| 26 | + # or starts with "xr" or "yt", |
| 27 | + # add an "ay" sound to the end of the word. |
| 28 | + if is_rule_1(text): |
| 29 | + return text + 'ay' |
| 30 | + |
| 31 | + # Rule 2 |
| 32 | + # If a word begins with one or more consonants, first move those consonants |
| 33 | + # to the end of the word and then add an "ay" sound to the end of the word. |
| 34 | + if is_rule_2(text): |
| 35 | + i = get_last_consonant_indx(text) |
| 36 | + return text[i + 1:] + text[: i + 1] + 'ay' |
| 37 | + |
| 38 | + # Rule 3 |
| 39 | + # If a word starts with zero or more consonants followed by "qu", first move |
| 40 | + # those consonants (if any) and the "qu" part to the end of the word, and then |
| 41 | + # add an "ay" sound to the end of the word. |
| 42 | + if is_rule_3(text): |
| 43 | + i = text.index('qu') |
| 44 | + return text[i + 2:] + text[:i + 2] + 'ay' |
| 45 | + |
| 46 | + # Rule 4 |
| 47 | + # If a word starts with one or more consonants followed by "y", first move the |
| 48 | + # consonants preceding the "y" to the end of the word, and then add an "ay" sound |
| 49 | + # to the end of the word. |
| 50 | + if is_rule_4(text): |
| 51 | + i = text.index('y') |
| 52 | + return text[i:] + text[:i] + 'ay' |
| 53 | + |
| 54 | + |
| 55 | +def is_rule_1(text: str) -> bool: |
| 56 | + """ |
| 57 | + Check if a word begins with a vowel, or starts with "xr" or "yt". |
| 58 | +
|
| 59 | + :param text: |
| 60 | + :return: |
| 61 | + """ |
| 62 | + if is_vowel(text[0]): |
| 63 | + return True |
| 64 | + if text[:2] in ("xr", "yt"): |
| 65 | + return True |
| 66 | + return False |
| 67 | + |
| 68 | + |
| 69 | +def is_rule_2(text: str) -> bool: |
| 70 | + """ |
| 71 | + Check ff a word begins with one or more consonants. |
| 72 | + No 'qu' or 'y' in it. |
| 73 | +
|
| 74 | + :param text: |
| 75 | + :return: |
| 76 | + """ |
| 77 | + return is_consonant(text[0]) and not is_rule_3(text) and not is_rule_4(text) |
| 78 | + |
| 79 | + |
| 80 | +def is_rule_3(text: str) -> bool: |
| 81 | + """ |
| 82 | + Check if a word starts with zero or more consonants followed by "qu". |
| 83 | +
|
| 84 | + :param text: |
| 85 | + :return: |
| 86 | + """ |
| 87 | + if 'qu' in text: |
| 88 | + if text[:2] == 'qu': |
| 89 | + return True |
| 90 | + |
| 91 | + for char in text[:text.index('qu')]: |
| 92 | + if is_vowel(char): |
| 93 | + return False |
| 94 | + return True |
| 95 | + return False |
| 96 | + |
| 97 | + |
| 98 | +def is_rule_4(text: str) -> bool: |
| 99 | + """ |
| 100 | + Check if a word starts with one or more consonants followed by "y". |
| 101 | +
|
| 102 | + :param text: |
| 103 | + :return: |
| 104 | + """ |
| 105 | + if 'y' in text and text[0] != 'y': |
| 106 | + for char in text[:text.index('y')]: |
| 107 | + if is_vowel(char): |
| 108 | + return False |
| 109 | + return True |
| 110 | + return False |
| 111 | + |
| 112 | + |
| 113 | +def is_vowel(char: str) -> bool: |
| 114 | + """ |
| 115 | + Test that char in vowels: the letters a, e, i, o, and u. |
| 116 | +
|
| 117 | + :param char: |
| 118 | + :return: |
| 119 | + """ |
| 120 | + return char in "aeiou" |
| 121 | + |
| 122 | + |
| 123 | +def is_consonant(char: str) -> bool: |
| 124 | + """ |
| 125 | + Check that char is consonant (the other 21 letters of the English alphabet). |
| 126 | +
|
| 127 | + :param char: |
| 128 | + :return: |
| 129 | + """ |
| 130 | + return char.isalpha() and not is_vowel(char) |
| 131 | + |
| 132 | + |
| 133 | +def get_last_consonant_indx(text: str) -> int: |
| 134 | + """ |
| 135 | +
|
| 136 | + :param text: |
| 137 | + :return: |
| 138 | + """ |
| 139 | + i = 0 |
| 140 | + for i, char in enumerate(text): |
| 141 | + if is_consonant(char): |
| 142 | + i += 1 |
| 143 | + else: |
| 144 | + break |
| 145 | + |
| 146 | + return i - 1 |
0 commit comments