Question

In: Computer Science

question: Pig LatinPig latin, is a mock language which encrypts english sentences using the following rules:...

question: Pig LatinPig latin, is a mock language which encrypts english sentences using the following rules: For every word in a sentence, the firstletter of the word is shifted to the end of the word and the suffix “-ay” is added at the end of the word. For example, “hey pat”becomes “eyhay atpay”.Implement thepiglatin(str)function so it translates sentences to pig latin. If you have time remaining, try implementing afunction to ”decrypt” piglatin, i.e. given a sentence in piglatin, return the english version of the sentence

def pig_latin(sentence: str) -> str:
    """
    Return a pig latin encrypted version of the given sentence

    >>> pig_latin("that pig is so cute aww")
    'hattay igpay siay osay utecay wwaay'

    >>> pig_latin("utm needs more food options")
    'tmuay eedsnay oremay oodfay ptionsoay'
    """
    # TODO: write this function's body - ay
    pass

Solutions

Expert Solution

Code is well explained in comments=>

#This is Encryption Function
def pig_latin(sentence: str):
    #sentence passed for encryption
    list1=sentence.split(' ')
    #split the sentence by space and store them in list
    #as we have to encrypt each word in sentence
    #Iteration of the list of sentence 
    for i in range(len(list1)):
        prevStr=list1[i]
        #store word in prevStr(temporary string) for encryption
        updateStr=prevStr[1:]+prevStr[0]+"ay"
        # as given in question we will take all character in word
        #except 1st by using prevStr[1:] and put first letter of word
        # after it by using prevStr[0] and at last add "ay" at the end
        # finally store them in updateStr
        list1[i]=updateStr
        #once encrytion is done we will store updateStr in list of sentence
    sentence=" ".join(list1)
    #once complete list of sentence is encypted we will convert it 
    #into string of sentence by using join(by space)
    return sentence
#This is decryption Function
def pig_latin_decryption(sentence: str):
    #encrypted text passed for decryption
    list1=sentence.split(' ')
    #split the sentence by space and store them in list
    #as we have to decrypt each word in encrypted text
    #Iteration of the sentence list
    for i in range(len(list1)):
        prevStr=list1[i]
        #store word in prevStr(temporary string) for decryption
        updateStr=prevStr[-3]+prevStr[:len(prevStr)-3]
        # By basic logics we will take last 3rd character in word 
        #by using prevStr[-3] and then add all characters of
        #word except last three by using prevStr[:len(prevStr)-3]
        # and finally store them in updateStr
        list1[i]=updateStr
    #once Decryption is completed we will convert it 
    #into string of sentence by using join(by space)
    sentence=" ".join(list1)
    return sentence
print("*****Encrypted Text*****")
print(pig_latin("that pig is so cute aww"))
print("*****Decrypted Text*****")
print(pig_latin_decryption("hattay igpay siay osay utecay wwaay"))
#some example

These code uses basic indexing and it is written for python 3.x.

In case of any doubt drop a comment


Related Solutions

Pig Latin is a language constructed by transforming English words. The following rules are used to...
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin: *If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”. *If the word begins with a vowel, then “way” is added to the end of the...
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...
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...
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...
You can turn a word into pig-Latin using the following two rules: If the word starts...
You can turn a word into pig-Latin using the following two rules: If the word starts with a consonant, move that letter to the end and append 'ay'. For example, 'happy' becomes 'appyhay' and 'pencil' becomes 'encilpay'. If the word starts with a vowel, simply append 'way' to the end of the word. For example, 'enter' becomes 'enterway' and 'other' becomes 'otherway'. For our purposes, there are 5 vowels: a, e, i, o, u (we consider y as a consonant)....
python Create a dictionary and insert several English words as keys and the Pig Latin (or...
python Create a dictionary and insert several English words as keys and the Pig Latin (or any other language) translations as values. Write a function called bonus that takes as a parameter a dictionary that has names as keys and salaries as values. Your function should increase everyone’s salary in the dictionary by 5%. Write a function called updateAge that takes as parameters a list of names of people whose birthday it is today, and a dictionary that has names...
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...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
Write a program that translates an English word into a Pig Latin word. Input ONE, and...
Write a program that translates an English word into a Pig Latin word. Input ONE, and ONLY one, word from the user, and then output its translation to Pig Latin. The rules for converting a word to Pig Latin follow. The general form for converting a word is to remove the first letter. Then append to the end of the word a hyphen, followed by the first letter that was removed, followed by "ay". For example, "robot" becomes "obot-ray" and...
Make a list of French and Latin vocabulary which entered the English language during the Middle English period
Make a list of French and Latin vocabulary which entered the English language during the Middle English period (after the Norman Conquest) and divide it into at least 6 categories such as military, law, government, architecture, household, food, and religion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT