Skip to content

Commit 32b8ab9

Browse files
committed
Merge branch 'main' of https://github.com/ikostan/python
2 parents d25dcad + ec71c7d commit 32b8ab9

File tree

5 files changed

+762
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)