In a challenge of CodingGame, there is a challenge that ask you to decode a string composed by reversed words ('Hello world' > 'olleH dlrow') then each characters converted into ascii value separeted by space:

>>> decode('111 108 108 101 72 32 100 108 114 111 119 32 41 58')
Hello world :)

The challenge is to be the fastest to write the decode function, but I later tried to write it with the fewest character possible.

Here is my result (104 characters):

def decode(m,s=' '):return s.join([w[::-1]for w in(''.join([chr(int(c))for c in m.split(s)]).split(s))])

The name of the function doesn't matter, it can be shorter, but only rename variable isn't good enough for an answer.

Is it possible to improve this result ?

  • \$\begingroup\$ Does the function have to be called decode? Is a specific version required? Can the output contain whitespace that isn't spaces, and if so, how should it be handled? \$\endgroup\$
    – xnor
    Dec 20, 2019 at 11:45
  • \$\begingroup\$ This part is obviously longer than a 'd' or something else, and does not required any knowledge, so I don't care (the "Take input from STDIN and output to STDOUT" part of the Jo King answer is pertinent). It must contain the ascii character '32'. \$\endgroup\$ Dec 20, 2019 at 11:52
  • 1
    \$\begingroup\$ To clarify, is this challenge limited to only Python? Also is I/O restricted to a function that takes as input an ASCII space character delimited string? Not Default I/O methods? \$\endgroup\$
    – 640KB
    Feb 20, 2020 at 16:56
  • \$\begingroup\$ This challenge is ok for default I/O method, but I'm new to this SE, sorry. I will edit the questions since all answer provide the default solution too. And thanks for the link about "default I/O methods! \$\endgroup\$ Feb 21, 2020 at 0:02

3 Answers 3


Use lambdas

def f(a,b):return c

can be shortened to

f=lambda a,b:c

(the f= can also be removed per CGCC site standards)

Remove excess []s

x.join([a for b in c])

can be

x.join(a for b in c)

since join can take a generator instead of a list

Split is on spaces by default

Thanks ElPedro for reminding me about this

So the split(' ')s can be shortened, which in turn means the s=' ' doesn't help (and it only broke even in the first place).

Take input from STDIN and output to STDOUT

print will automatically print arguments with space as a separator if you use * to unpacks the sequence/collection into positional arguments, and taking input via input() is shorter. Also, setting s to space doesn't actually save any bytes.

Altogether, this is 79 bytes:

print(*[w[::-1]for w in(''.join(chr(int(c))for c in input().split()).split())])

Try it online!

or, if you're dedicated to having a function, 82 bytes:

lambda m:' '.join(w[::-1]for w in(''.join(chr(int(c))for c in m.split()).split()))

Try it online!

plus seven or so characters if you really need the function to be called decode

  • 2
    \$\begingroup\$ Python splits on space by default so you can lose another 6 Try it online! :) \$\endgroup\$
    – ElPedro
    Dec 20, 2019 at 11:30
  • \$\begingroup\$ The actual challenge works with input() and print(), so in fact, this problem can be solved in 79 bytes on CodingGame. \$\endgroup\$ Dec 20, 2019 at 11:47

Python 2, 78 bytes

lambda m:' '.join(x[::-1]for x in''.join(map(chr,map(int,m.split()))).split())

Try it online!

Edit changed to a Python 2 answer as the lambda is shorter than the print/input version in Python 2.

This uses a couple of maps to get the list of characters which we then join on "", split again on space, reverse each element then rejoin again on space for 78. Also works in Python 3.

Just a pity that it needs so many brackets :-(

  • \$\begingroup\$ and 75 with print and input: print(*[x[::-1]for x in''.join(map(chr,map(int,input().split()))).split()]) \$\endgroup\$ Dec 20, 2019 at 12:11
  • 1
    \$\begingroup\$ I missed that as I am more familiar with Python 2 in which the print and input version comes out at 79 Try it online! \$\endgroup\$
    – ElPedro
    Dec 20, 2019 at 12:17
  • \$\begingroup\$ 80 in fact as I had missed out the space Try it online! \$\endgroup\$
    – ElPedro
    Dec 20, 2019 at 12:28

Python 3, 75 bytes

lambda a:' '.join(''.join(map(chr,map(int,a.split())))[::-1].split()[::-1])

Try it online!

Python 3, 70 bytes

If taking input from STDIN and outputing to STDOUT is fine then it takes only 70 bytes this is mostly because the spread operator (*) is shorter than ' '.join()


Try it online!

  • \$\begingroup\$ Your solution in 70 bytes works, also, for your lambda in 75 is good too. \$\endgroup\$ Feb 17, 2020 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.