Need help writing Pig Latin translation code in python

I have a task in which I have to translate english words into pig latin, this means if a word begins with a vowel the word has "ay" added onto the end ("apple" would become "appleay") this wasn't a problem as the code was relatively easy to write.
However, the second part is that if the word begins with a consonant, all consonants before the first vowel are removed and added onto the end of the word, with the string "ay" again also being added onto the end after that ("cheese" would become "eesechay").

It's a fairly simple concept but I'm struggling to find a way to translate the word if the word begins with a consonant, here is my code so far:

def pigLatin(word): for l in vowels: if word[0] == l: word = word + "ay" for L in consonants: if word[0] == L: for i in vowels: for s in word: if s == i: #this is where im completely lost 

FYI, vowels and consonants are arrays just containing the vowels and consonants, word is inputted by the user.

edit:

thank you for the help, I've managed to re do the code with and get something that works:

def pigLatin(word): if word[0]in vowels: word = word + "ay" elif word[0] in consonants: c = "" for l in word: if l in vowels: break elif l in consonants: c = c + l word = word[len(c)-len(word):len(word)] word = word + c + "ay" 

again, thank you for the help :)

8

2 Answers

Here are a few things that may help:

ascii_lowercase in the string module is a predefined string containing all lowercase alpha characters:

>>> from string import ascii_lowercase >>> ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> 

We can generate the set of all consonants, by creating a set of vowels, and taking the difference between the vowels and all characters:

from string import ascii_lowercase as alphabet vowels = set("aeiou") consonants = set(alphabet) ^ vowels print(consonants) 

Output:

{'c', 's', 'q', 'm', 'g', 'd', 'y', 'l', 'b', 'k', 't', 'j', 'r', 'p', 'h', 'v', 'n', 'w', 'z', 'f', 'x'} >>> 

Since this is a set, there is no intrinsic order, but that doesn't matter. If we want to know if a given character is a consonant or vowel, we simply check for membership with the corresponding set (you can do the same with lists, but a set would be the preferred data-structure).

Regardless of whether or not you're using lists or sets for your vowels and consonants, you can simplify your code by simply checking for membership (checking to see if a character is within a collection):

if word[0] in vowels: # The first letter is a vowel elif word[0] in consonants: # The first letter is a consonant 

If you know in advance that word will only contain lowercase alpha characters (no special symbols, digits, uppercase letters, etc.), then you could simplify it further:

if word[0] in vowels: # The first letter is a vowel else: # If it's not a vowel, it must be a consonant 

However, if you think about it, you don't really need to check if the first letter is a vowel, at all. You already know that you'll be adding "ay" at the end of the final string, regardless of whether or not the first letter is a vowel or consonant - so, you really just need to check if the first letter is a consonant.

Using everything so far, I would arrive at the following pseudo-code:

def to_pig_latin(word): from string import ascii_lowercase as alphabet vowels = set("aeiou") consonants = set(alphabet) ^ vowels if word[0] in consonants: # Do something return ... + "ay" 

I've renamed the function to_pig_latin, since snake-case is preferred, and prefixing the function name with to indicates that you are translating/transforming something. I've also moved the creation of vowels and consonants into the function, just because there's no reason to have it outside of the function, and it's cuter this way.

1

I agree with Charles Duffy's comment that we don't design programs for you. However, you're going down the wrong rabbit hole and I think you need a little guidance. Here is an example of what I was talking about. There are many ways of doing this, this is a simple solution (one of many).

def pigLatin(word): vowels = list("aeiou") consonants = list("bcdfghjklmnpqrstvwxyz") if word[0] in vowels: word = word + "ay" else: for counter, letter in enumerate(list(word)): if letter in vowels: word = word[counter:] + word[:counter] + "ay" break return word print(pigLatin("art")) print(pigLatin("donkey")) 

What if the word passed into pigLatin contains upper case characters? You could modify the function by converting everything to lowercase (or upper, your preference).

def pigLatin(word): vowels = list("aeiou") consonants = list("bcdfghjklmnpqrstvwxyz") if word[0].lower() in vowels: word = word + "ay" else: for counter, letter in enumerate(list(word)): if letter.lower() in vowels: word = word[counter:] + word[:counter] + "ay" break return word 

Do you get an idea of how much simpler and flexible this code is?

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like