Question

In: Computer Science

Use python write a function that translates the input string into Pig Latin. The translation should...

Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string.

The Pig Latin rules are as follows:

  1. For words that begin with consonants, all letters before the initial vowel are placed at the end of the word sequence. Then, “ay” is added.

Example: “pig” -> “igpay”, “banana” -> “ananabay”, “duck” -> “uckday”,

“stupid” -> “upidstay”, “floor” -> “oorflay”, “string” -> “ingstray”.

  1. For words that begin with vowels, append “yay” to the end of this word.

Example: “omelet” -> “omeletyay”, “eat” -> “eatyay”, “egg” -> “eggyay”.

  1. For whatever reason, we are afraid of 8-letter words. Thus, whenever you encounter a word with 8 letters, you should stop translating immediately and return what we have translated so far.

Example: “A short sentence” -> “Ayay ortshay”

  • Hint: command in might be useful here

  • Hint: either break or continue may be helpful here. Think which one

  • Hint: the method enumerate() can be useful. A brief explanation and examples of its potential use can be found here.

def pig_latin(string):

    """

    >>> pig_latin('Hi how are you')

    'iHay owhay areyay ouyay'

    >>> pig_latin('Absolute')

    ''

    >>> pig_latin('When words begin with consonant clusters')

    'enWhay ordsway eginbay ithway onsonantcay'

    """

Solutions

Expert Solution

Raw_code:

# pig_latin function defintion takes a string as parameter
def pig_latin(string):
# splitting the string into list of words at whitespace characters
string = string.split()
# a vowels tuple for comparision
vowels = ("a", "e", "i", "o", "u")
# result variable for storing result string
result = ""
# for loop for iterating over each word in the string
for word in string:
# if word is of length 8 breaking the function by returning result
if len(word) == 8:
return result
# using memebership operator and string index for checking whether
#the first character is an vowel
elif word[0] in vowels or word[0].lower() in vowels:
result += word + "yay "
else:
# for loop for iterating over the characters in the word until it reaches a vowel
for index, char in enumerate(word):
# using memebership operator and checking whether the character is an vowel
if char in vowels or char.lower() in vowels:
# using string slicing for getting result string
# result is string from vowel to last + string form start to vowel + "ay "
result += word[index:] + word[:index] + "ay "
# breaking for loop for the word
break
# returning result
return result

print(pig_latin("Hi how are you"))
print(pig_latin("When words begin with consonant clusters"))
print(pig_latin("pig"))
print(pig_latin("banana"))
print(pig_latin("duck"))
print(pig_latin("stupid"))
print(pig_latin("floor"))
print(pig_latin("string"))
print(pig_latin("omelet"))
print(pig_latin("eat"))
print(pig_latin("egg"))


Related Solutions

IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
Create a program that translates English into Pig Latin. The function will take the first letter...
Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case ​ins​ ensitive. You must write the functiontranslate() ​that takes in a single English word and ​returns​ its Pig Latin translation. Remembertranslatemusttake​onlyonewordasinput.​ ############################################################# # translate() takes a single english word translates it to #pig latin...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
How do I write a COBOL program that translates a word entered into pig Latin?
How do I write a COBOL program that translates a word entered into pig Latin?
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin...
Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin that doesn't allow follow all the Pig Latin Syntax rules.) Use predefined methods of the Array and String classes to do the work. For simplicity in your conversion, place the first letter as the last character in the word and prefix the characters "ay" onto the end. For example, the word "example" would become "xampleay" and "method" would become "ethodmay." Allow the user to...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT