Write a Python module called piglatin.py that contains functions for translating
ID: 3688598 • Letter: W
Question
Write a Python module called piglatin.py that contains functions for translating sentences between English and a variant of Pig Latin (see: http://en.wikipedia.org/wiki/Pig_Latin).
To convert from English to Pig Latin, each word must be transformed as follows:
if the word begins with a vowel, ‘way’ should be appended (example: ‘apple’ becomes ‘appleway’)
if the word begins with a sequence of consonants, this sequence should be moved to the end, prefixed with ‘a’ and followed by ‘ay’ (example: ‘please’ becomes ‘easeaplay’)
NB: Assume, when reverting Pig Latin to English that the original English text does not contain the letter "w".
The Python module will contain the following functions:
to_pig_latin(sentence)
Return the Pig Latin sentence for a given English sentence.
to_english(sentence)
Return the English sentence for a given Pig Latin sentence.
Sample I/O:
(E)nglish or (P)ig Latin?
E
Enter an English sentence:
the quick black fox jumps over the lazy apple
Pig-Latin:
eathay uickaqay ackablay oxafay umpsajay overway eathay azyalay
appleway
Sample I/O:
(E)nglish or (P)ig Latin?
P
Enter a Pig Latin sentence:
eathay uickaqay ackablay oxafay umpsajay overway eathay azyalay
appleway
English:
the quick black fox jumps over the lazy apple
Explanation / Answer
# Sample I/O:
# (E)nglish or (P)ig Latin?
# E
# Enter an English sentence:
# the quick black fox jumps over the lazy apple
# Pig-Latin:
# eathay uickaqay ackablay oxafay umpsajay overway eathay azyalay appleway
# Sample I/O:
# (E)nglish or (P)ig Latin?
# P
# Enter a Pig Latin sentence:
# eathay uickaqay ackablay oxafay umpsajay overway eathay azyalay appleway
# English:
# the quick black fox jumps over the lazy apple
def is_vowel(c):
vowels = "aeiou"
vowels.find(c.lower())
if vowels.find(c.lower()) == -1:
return False
else:
return True
#Filtering function
def toPigLatin (sent):
new_word = ''
dammit = ''
for a in sent.split(" "):
if is_vowel(a[0]):
a = a+"way "
dammit = dammit + a
else:
for i in range(len(a)):
if not is_vowel(a[i]):
if i == (len(a)-1):
new_word = a[i+1:]+"a"+a[0:i+1]+"ay "
dammit = dammit + new_word
new_word = a[i+1:]+"a"+a[0:i+1]+"ay "
else:
dammit = dammit + new_word
break
return dammit
def toEnglish (sent):
sec = ''
for a in sent.split(" "):
if a[-3:] == "way":
sec = sec + a[0:-3]+ " "
else:
n = -3
new_word = ''
while True:
if a[n] == 'a':
new_word = new_word+ a[:n] + " "
break
else:
new_word = a[n] + new_word
n = n-1
sec = sec + new_word
return(sec)
main.py
# test program for english/piglatin translator
import piglatin
choice = input ("(E)nglish or (P)ig Latin? ")
action = choice[:1]
if action == 'E':
s = input("Enter an English sentence: ")
new_s = piglatin.toPigLatin(s)
print("Pig-Latin:")
print(new_s)
elif action =='P':
s = input("Enter a Pig Latin sentence: ")
new_s = piglatin.toEnglish(s)
print("English:")
print(new_s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.