Decode ascii and reversed words sentence

10

0

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 ?

Dorian Turba

Posted 2019-12-20T10:13:13.420

Reputation: 263

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? – xnor – 2019-12-20T11:45:52.337

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'. – Dorian Turba – 2019-12-20T11:52:49.570

1

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?

– 640KB – 2020-02-20T16:56:08.490

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! – Dorian Turba – 2020-02-21T00:02:33.813

Answers

2

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()

print(*''.join(map(chr,map(int,input().split())))[::-1].split()[::-1])

Try it online!

Mukundan

Posted 2019-12-20T10:13:13.420

Reputation: 1 188

Your solution in 70 bytes works, also, for your lambda in 75 is good too. – Dorian Turba – 2020-02-17T09:17:31.637

6

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

Jo King

Posted 2019-12-20T10:13:13.420

Reputation: 38 234

2

Python splits on space by default so you can lose another 6 Try it online! :)

– ElPedro – 2019-12-20T11:30:41.520

The actual challenge works with input() and print(), so in fact, this problem can be solved in 79 bytes on CodingGame. – Dorian Turba – 2019-12-20T11:47:27.233

5

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 :-(

ElPedro

Posted 2019-12-20T10:13:13.420

Reputation: 5 301

and 75 with print and input: print(*[x[::-1]for x in''.join(map(chr,map(int,input().split()))).split()]) – Dorian Turba – 2019-12-20T12:11:23.607

1

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!

– ElPedro – 2019-12-20T12:17:26.977

80 in fact as I had missed out the space Try it online!

– ElPedro – 2019-12-20T12:28:54.037