Convert text to Pig Latin

Convert Text to Pig Latin

Computer Science

  1. Write a module, complement.py, with a function, complement(sequence) that takes a DNA sequence as string input and returns the complement of that string. Also provide a function, revComplement(sequence) that takes a DNA sequence as string input and returns the reverse complement of the sequence as a string.

Recall that the valid alphabet is {A, C, T, G} and that A-T and G-C are complements. A reverse complement is found by reversing the input string and replacing every nucleotide with its complement. This means that your revComplement method should use your complement method internally rather than duplicating code.

So, if your input is ACTG, your complement should be TGAC and your reverse complement should be CAGT.

Your methods should do appropriate error checking (the DNA string entered contains characters other than A, C, T, or G) and return an error message as appropriate. Test your functions with input from the user.

  1. Write a module, latin.py, with two functions wordToPig and nameToPig. Your wordToPig function will take 1 parameter and convert the word passed in to the parameter based on the below rules. Your nameToPig function takes 2 input parameters, firstName and lastName, and will use your wordToPig function to translate the names. Your nameToPig function returns the names in pig Latin.

Please use the following guidelines for Pig Latin:
• Words beginning with consonants: move the consonant from the start of the word

to the end of the word. Then add the suffix “ay” to the end of the word. For example, the word “hello” would become ellohay, the word “duck” would become uckday.

o For a bonus, remove all consonants from the start of the word to the end of the word and then add the suffix “ay” to the end. For example, “Chapman” becomes Apmanchay.

• Words beginning with vowels: all you need to do is add “yay” to the end of the word. You don’t need to change any letters around, just spell the word as normal then add “yay” to the end. For example: the word “egg” becomes eggyay and the word “ultimate” becomes ultimateyay.

Make sure to use string methods to have capital letters where appropriate and not otherwise. (“Hello” becomes “Ellohay” not “elloHay”.) Test your functions with input from the user. Assume the user enters his/her name with capitals for both their first and last name. (Daniele Struppa becomes Anieleday Truppasay – bonus for Anieleday Uppastray) 

Solution 

#checks if the first letter is a vowel,

#if it is then print it with “yay” at the end

#otherwise,keep checking the list and pushing non-vowels to the back

#once encountered a vowel,capitalize it and return the transformed word

#+ the suffix “ay”

def wordToPig(name):

if name[0] in [‘A’,’E’,’I’,’O’,’U’]:

return name+’yay’

else:

while name[0] not in [‘A’,’E’,’I’,’O’,’U’,’a’,’e’,’i’,’o’,’u’]:

name = name[1:]+name[0].lower()

name += ‘ay’

return name[0].upper()+name[1:]

#applies the above function to 2 arguements

def nameToPig(first,last):

return wordToPig(first)+’ ‘+ wordToPig(last)

#asks the user which function they want to run and what arguements to use

print(“type ‘1’ to run the wordToPig function,type ‘2’ to run the nameToPig function”)

choice = input()

if choice != ‘1’ and choice != ‘2’:

raise Exception(“Please pick 1 or 2”)

if choice == ‘1’:

print(‘enter the name you would like to apply the function to’)

arg = input()

print(wordToPig(arg))

if choice == ‘2’:

print(‘enter the first name you would like to apply the function to’)

arg = input()

print(‘enter the second name you would like to apply the function to’)

arg2 = input()

print(nameToPig(arg,arg2))