KittyWhale
KittyWhale

Reputation: 89

Need help to translate a string to pyg latin

I want to write a function that will take a string and turn the words into Pyg Latin. That means that:

  1. If a word begins with a vowel, add "-way" to the end. Example: "ant" becomes "ant-way".
  2. If a word begins with a consonant cluster, move that cluster to the end and add "ay" to it. Example: "pant" becomes "ant-pay". I've searched many posts and websites but none of them do the same way or the way I want to do it. I have to test these functions in a test and I have 4 test cases for this one. One is 'fish' and it should returns 'ish-fray' the second is 'frish' and it should returns 'ish-fray' the third is 'ish' and it should return 'ish-way' and the last is 'tis but a scratch' and it should return 'is-tay ut-bay a-way atch-scray'

I've found a program that can translate it CLOSE to what it has to be but I'm not sure how to edit it so it can return the result I'm looking for.

def pyg_latin(fir_str):
 pyg = 'ay'
 pyg_input = fir_str
 if len(pyg_input) > 0 and pyg_input.isalpha():
    lwr_input = pyg_input.lower()
    lst = lwr_input.split()
    latin = []
    for item in lst:
        frst = item[0]
        if frst in 'aeiou':
            item = item + pyg
        else:
            item = item[1:] + frst + pyg
        latin.append(item)
    return ' '.join(latin)

So, this is the result my code does:

pyg_latin('fish')
#it returns
'ishfay'

What I want it to return isn't much different but I dont know how to add it in

pyg_latin('fish')
#it returns
'ish-fay'

Upvotes: 3

Views: 120

Answers (1)

Nathan
Nathan

Reputation: 3200

Think about what the string should look like.

Chunk of text, followed by a hyphen, followed by the first letter (if it’s a not a vowel), followed by “ay”.

You can use python string formatting or just add the strings together:

Item[1:] + “-“ + frst + pyg 

It is also worth learning how array slicing works and how strings are arrays that can be accessed through the notation. The following code appears to work for your test cases. You should refactor it and understand what each line does. Make the solution more robust but adding test scenarios like '1st' or a sentence with punctuation. You could also build a function that creates the pig latin string and returns it then refactor the code to utilize that.

def pg(w):

    w = w.lower()
    string = ''
    if w[0] not in 'aeiou':

        if w[1] not in 'aeiou':
            string = w[2:] + "-" + w[:2] + "ay"
            return string
        else:
            string = w[1:] + "-" + w[0] + "ay"
            return string
    else:
        string = w + "-" + "way"
        return string

words = ['fish', 'frish', 'ish', 'tis but a scratch']

for word in words:
    # Type check the incoming object and raise an error if it is not a list or string
    # This allows handling both 'fish' and 'tis but a scratch' but not 5.
    if isinstance(word, str):
        new_phrase = ''
        if ' ' in word:
            for w in word.split(' '):
                new_phrase += (pg(w)) + ' '

        else:
            new_phrase = pg(word)
        print(new_phrase)

    # Raise a Type exception if the object being processed is not a string
    else:
        raise TypeError

Upvotes: 1

Related Questions