In: Computer Science
You can turn a word into pig-Latin using the following two rules:
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'
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.