Hrishi
Hrishi

Reputation: 51

Translating a string to pig latin

So I recently came to know about the asterisk function in Python which is used to unpack the values. I tried to implement it in my code for the given question below. But unfortunately, I am getting this error:

Traceback (most recent call last):
  File "tests.py", line 1, in <module>
    from solution import *
  File "/workspace/default/solution.py", line 2
    return *[x[1:]+x[0]+'ay' for x in text.split()]
           ^
SyntaxError: can't use starred expression here

Q.Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples

pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !')     # elloHay orldway !

My Code:

def pig_it(text):
    return *[x[1:]+x[0]+'ay' for x in text.split()]

Upvotes: 0

Views: 348

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114330

You need str.isalpha or similar, and str.join:

return ' '.join(x[1:] + x[0] + 'ay' if x.isalpha() else x for x in text.split())

This does not check for punctuation attached to a word.

Star expansion, or splat, is totally irrelevant in this case. It deals the elements of an iterable into a sequence or argument list. Since you don't really have a sequence or argument list anywere, you don't need or want splat.

Upvotes: 1

Related Questions