-4

Today i'll publish a riddle.

The best answer will be the shortest code.

Write a one liner function that accepts a currency and a list of numbers. The function needs to return a string where each number from the list is attached with its currency.

Here's an example for a simple way to solve it.

def combine_coin(coin, numbers):
    coins_str = ''
    for num in numbers:
        coins_str += coin + str(num) + ', '
    return coins_str[:-2]

print(combine_coin('$', range(5)))
jia Jimmy
  • 1,693
  • 2
  • 18
  • 38
T.Stimer
  • 255
  • 1
  • 2
  • 8

3 Answers3

1
def combine_coin(coin, numbers):
    return ', '.join([f'{coin}{k}' for k in numbers])
Gil Pinsky
  • 2,388
  • 1
  • 12
  • 17
0
print(','.join(list(map(lambda num:"$"+num,input("enter the values <sep by space>").split()))))

Okay splitting this long line, we get 1) ','.join(list( - this will join the list we get with a comma

2)map- maps a function to all values in a list and returns a map object containing the return value of the function

3)lambda num:'$'+str(num) - takes a number and returns its string with a '$' example: '$1'

4)input().split()- splits the input by space

  • Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Sep 24 '20 at 07:41
0

Codegolf is fun:

def combine_coin(c, n):
    return ', '.join(c+str(k) for k in n)
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28