Question

In: Computer Science

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).

Write a function pig() that takes a word (i.e., a string) as input and returns its pig-Latin form. Your function should still work if the input word contains upper case characters. Your output should always be lower case.

For example, if you enter

pig('happy')

The output should be

'appyhay'

If you enter

pig('Enter')

The output should be

'enterway'

Solutions

Expert Solution

In order to solve this problem, one should need to know about functions in python, some basic methods like split(), join(), lower(), append(), etc.

Syntax of Creating and calling a function in python:

def Function_Name():   #Function Creating
   print("Hello World")

Function_Name()  #Function Calling

split() method in python is used to split the text or word to list. for example:

txt = "Hello hi"
txt.split(" ")  #split the txt to list

Output:

join() method in python joins all the items to a string. for example:

txt = ['Hello','hi']  #txt list
c = " ".join(txt)   #applying join method
print(c)

Output:

lower() method in python is used to return a string with all characters in lower case. for example:

txt = "HELLOHI"  #txt in captial format
txt.lower()  #applying lower method

Output:

append() method in python adds an item at the end of list. for example:

txt = ['Hello','hi']
txt.append('hey') #applying append method
print(txt)

Output:

Python Code of given problem:

# creating function pig
def pig(piglatin):
    #applying lower() method so that if input contains Uppercase letter it get converted back 
    #to lower case.applying split() method so that to convert input word to list
    CompleteWord = piglatin.lower().split(" ") 
    # declaring Vowels i.e 'a','e','i','o', 'u' 
    Vowels = ['a', 'e', 'i', 'o', 'u']
    #empty piglatin
    piglatin = []
    for Word in CompleteWord:
        # if Word[0] (i.e first word) of input is in 'aeiou' that is vowel is first
        if Word[0] in 'aeiou': 
            piglatin.append(Word + 'way') #append method append 'way' at last
        else:
            for letter in Word:
               if letter in 'aeiou': 
                  piglatin.append(Word[Word.index(letter):] + Word[:Word.index(letter)] +'ay')
                  break
        #here, piglatin.append(Word[Word.index(letter):] + Word[:Word.index(letter)] +'ay')
        #can be summarised as an example i.e word 'Enter' => 'nter' + 'e' + 'ay' i.e 
        #piglatin.append(Word[Word.index(letter):]) append the letter after first word and 
        #Word[:Word.index(letter)] is first word and further 'ay' os adding at last.
    print(" ".join(piglatin)) #join method add all the items of list

pig('Enter')  # calling pig function
    
    

The output of calling pig('Enter') is :

Similarly, the Output of calling pig('happy') is:

Similarly, the Output of calling pig('Pencil') is:

The function will still work if the input contains upper case letter i.e:

the Output of calling pig('HAPPY') is:

so, this is how one can turn a word to pigLatin.


Related Solutions

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...
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...
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...
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?
How would you go about writing a MATLAB program to convert a phrase in Pig Latin...
How would you go about writing a MATLAB program to convert a phrase in Pig Latin to English. I have already written a script that converts phrases from English into Pig Latin; however, I am not sure how to reverse the process. Can anyone who know's MATLAB please help me out, thank you? P.S. I know that this is an unambigious task so it doesn't have to work completely well. With the most minor assumptions made, if it could covert...
Name a type of document that can be created with Microsoft Word. Using what you have...
Name a type of document that can be created with Microsoft Word. Using what you have learned from practice, describe the functions of Microsoft Word that aid you in creating the document you named. Describe how you would use the Word functions for creating your document.
Do the following using R. You must also turn in a copy of your R code....
Do the following using R. You must also turn in a copy of your R code. (10) What is the probability a beta (1, 8) random variable is less than 0.13? (11) What is the probability a beta (3, 9) random variable is greater than .4? (12) What is the probability a beta (18,4.4) random variable is between 0.6 and 0.7? (13) At what value of x is the probability that a beta (4, 7) random varable is less than...
Using only the rules of SD, derive the following rules of SD+: a) Disjunctive Syllogism (one...
Using only the rules of SD, derive the following rules of SD+: a) Disjunctive Syllogism (one case) : P v Q ~P b) Commutation: (wedge only) : P v Q <--> Q v P c) Implication : P ⊃ Q <--> Q ⊃ ~P d) Double Negation P <--> ~~P e) DeMorgan (~(P & Q) <--> ~P v ~Q only) Use SL sentences rather than metavariables in your derivations. Each direction of replacement rules must be shown (that is, you...
For questions (10 )   t through (25) you write correct word for the blank space(YES STARTS...
For questions (10 )   t through (25) you write correct word for the blank space(YES STARTS AT 14, ALREADY HAVE 1-13) Long run market demand must be (14)______for a Cartel to survive and do well. Long run market supply must be (15)________for a Cartel to survive and do well. According to Hotellings Paradox, an industry with a small number of sellers with large market shares will often end up offering products that are highly (16)_______ and are aimed at the...
Word Building Build the following terms using word parts. 1. pus in the uterus: word part...
Word Building Build the following terms using word parts. 1. pus in the uterus: word part for pus ________________________ word part for uterus ________________________ term for pus in the uterus ________________________ 2. near the ovary: word part for near ________________________ word part for ovary ________________________ term for near the ovary ________________________ 3. pertaining to the urinary and reproductive systems: word part for urinary ________________________ word part for reproductive system ________________________ word part for pertaining to ________________________ term for pertaining to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT