Spoiler alert if you are doing code wars and haven't completed 5kyu Simple Pig Latin kata
All you must do here is:
"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"
For example for input 'Pig latin is cool' it should return 'igPay atinlay siay oolcay' and for 'This is my string' it should return 'hisTay siay ymay tringsay'
I tried to do a one-liner solution and my code returns syntax error when I run it and I have no idea why.
def pig_it(text):
return ' '.join([word[1:] + word[0] + 'ay' for word in text.split() if word not in ',!?' else word])
The problem is with else word
, my code doesn't build at all, why is this else statement causing a problem?
My code works fine when it looks like this
def pig_it(text):
return ' '.join([word[1:] + word[0] + 'ay' for word in text.split() if word not in ',!?'])
It doesn't solve all tests though (whenever there is a comma, question mark or exclamation mark)