0

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()]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Hrishi
  • 41
  • 9
  • 2
    What do you expect the `*` to do here? – Klaus D. Jul 21 '21 at 05:07
  • I am expecting a string containing the elements in the list. It should be same as the output given by: ' '.join([x[1:]+x[0]+'ay' for x in text.split()]) – Hrishi Jul 21 '21 at 05:09
  • 2
    That is not a function, it is part of the language syntax, and what it means depends on context, but it isn't a function (although sometimes an operator) – juanpa.arrivillaga Jul 21 '21 at 05:10
  • If a=[1,2,3,4]. Then print(*a) gives '1 2 3 4' right? – Hrishi Jul 21 '21 at 05:10
  • It is totally unclear *waht* you expect `*` to do in `return *some_list` – juanpa.arrivillaga Jul 21 '21 at 05:10
  • Yes * is a part of the syntax. So why exactly isn't it not working here? Could you please explain. – Hrishi Jul 21 '21 at 05:11
  • 1
    @Hrishi that's because `*` *unpacks an interable as the positional arguments to a function call, in this case `print`*, it has nothing to do with `''.join` – juanpa.arrivillaga Jul 21 '21 at 05:11
  • What I am expecting is the output (given as a comment) beside the examples – Hrishi Jul 21 '21 at 05:12
  • 1
    @Hrishi the *problem* is that `return *whatever` **doesn't make sense**. It isn't valid syntax, as the error states. You seem to misunderstand what `*` is doing in `print(*whatever)` – juanpa.arrivillaga Jul 21 '21 at 05:12
  • @juanpa.arrivillaga So brother can you tell me if I am correct about the concept. * combines the elements in a given lists into a string where each of the elements is seperated by a space. Like, arr = ['sunday', 'monday', 'tuesday', 'wednesday'] and if we do print (*arr) we get "sunday monday tuesday wednesday". Is it correct? I tried the same logic here except that I returned it. – Hrishi Jul 21 '21 at 05:15
  • What is your source for claiming *"\* combines the elements in a given lists into a string"*? It doesn't. `print` can take a variable number of arguments, and `*` spreads (spashes, unpacks, ...) values to become multiple arguments. There is no string conversion nor concatenation happening. – trincot Jul 21 '21 at 05:17
  • 1
    @Hrishi "* combines the elements in a given lists into a string where each of the elements is seperated by a space" That is *totally wrong*. This is what I'm telling you, you are misunderstanding what is happening. See [this question](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) and [this question](https://stackoverflow.com/questions/6967632/unpacking-extended-unpacking-and-nested-extended-unpacking) – juanpa.arrivillaga Jul 21 '21 at 05:19
  • Thank you trincot & juanpa.arrivillaga. def pig_it(text): a=[x[1:]+x[0]+'ay' for x in text.split()] print(*a) print(pig_it('Pig latin is cool')) ''' Output igPay atinlay siay oolcay None ''' This code kinda made me misunderstand the whole concept. When I saw this I thought the same statement inside the print could be used inside a return statement. – Hrishi Jul 21 '21 at 05:27

1 Answers1

1

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264