Question

In: Computer Science

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.

  • For the above you can assume that ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ are vowels, and any other letter is a consonant. This does mean that ‘y’ is considered a consonant even in situations where it really shouldn’t be.

For this exercise, you can assume the following:

  • There will be no punctuation.

  • All letters will be lowercase

  • Every word will have at least one vowel (so we won’t give you a word like “by”)

Write a helper function that finds the index of the first vowel in a given word, and use that in your main function.

Hints:

  • To find the index of the first vowel in a given word, since you’re interested in the indexes, looping through the indexes of the string using range, or enumerate, or a while loop may work better than a direct for loop on the characters.

  • Use slicing to break up the string into all of the letters before the vowel, and all of the letters from the vowel onwards.

Examples:

>>> igpay('can')

'ancay'

>>> igpay('answer')

'answerway'

>>> igpay('prepare')

'eparepray'

>>> igpay('synthesis')

'esissynthay'

Solutions

Expert Solution

CODE:

def first_vowel(word):                   # A HELPER FUNCTION WHICH RETURNS THE INDEX OF FIRST VOWEL IN THE WORD
    vowels = "aeiou"                     # STORED THE VOWELS IN A STRING VARIABLE VOWELS
    for index,value in enumerate(word):  # ENUMERATE FUNCTION DOES THE INDEXING FOR THE WORD, FOR EX. ENUMERATE("HELLO") = [(0, 'H'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')] MEANS THE FIRST VALUE IS INDEX AND 2ND VALUE IS THE VALUE AT THAT INDEX
        
        if value in vowels:              # CHECK IF VALUE IS IN VOWELS, THEN RETURN INDEX ELSE DO NOTHING, IN LAST IF VOWEL NOT FOUND RETURN -1 
            return index
    return -1


def igpay(word):                        # FUNCTION WHICH RETURNS THE STRING TRANSLATED TO PIG LATIN
    index = first_vowel(word)           # FIND THE INDEX OF FIRST VOWEL
    
    res = ""                            # INITIALIZE THE RESULTANT STRING RES WITH EMPTY STRING
    
    if index>0:                         # IF INDEX IS GREATER THAN 0 THAT MEANS WORD START WITH A CONSONANT
        
        res+=word[index:]+word[:index]+"ay" # ASSIGN ALL THE LETTERS FROM INDEX TO LAST, AND CONCATENATE AFTER START TO INDEX AND IN LAST AY
        
    elif index==0:                      # IF INDEX IS 0 THAT MEANS WORD STARTS WITH A VOWEL
        res+=word+"way"                 # SO JUST CONCATENATE WAY IN THE LAST OF THE WORD
        
    return res                           # RETURN THE RESULTANT STRING

# SAMPLE INPUT      
print(igpay('can'))
print(igpay('answer'))
print(igpay('prepare'))
print(igpay('synthesis'))

OUTPUT:

ancay
answerway
eparepray
esissynthay

NOTE: If you have any queries regarding the solution, you may ask in the comment section. HAPPY LEARNING!!


Related Solutions

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: For words that begin with consonants, all letters before the initial vowel...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which words in English are altered, usually by removing letters from the beginning of a word and arranging them into a suffix. The rules we will use for the pig-latin in this program are as follows: If a word starts with a consonant, split the word at the first instance of a vowel, moving the beginning consonants to the end of the word, following a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
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...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT