From text to ascii art from CodinGame practice puzzle

3

On CodingGame, there is a Puzzle that ask to write words with ascii art. I solve the puzzle then tried to golf it. What improvement can I do on it?

INPUT
>>> 20
>>> 11
>>> MANHATTAN
>>>   .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .-----------------. .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------. 
>>> | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
>>> | |      __      | || |   ______     | || |     ______   | || |  ________    | || |  _________   | || |  _________   | || |    ______    | || |  ____  ____  | || |     _____    | || |     _____    | || |  ___  ____   | || |   _____      | || | ____    ____ | || | ____  _____  | || |     ____     | || |   ______     | || |    ___       | || |  _______     | || |    _______   | || |  _________   | || | _____  _____ | || | ____   ____  | || | _____  _____ | || |  ____  ____  | || |  ____  ____  | || |   ________   | || |    ______    | |
>>> | |     /  \     | || |  |_   _ \    | || |   .' ___  |  | || | |_   ___ `.  | || | |_   ___  |  | || | |_   ___  |  | || |  .' ___  |   | || | |_   ||   _| | || |    |_   _|   | || |    |_   _|   | || | |_  ||_  _|  | || |  |_   _|     | || ||_   \  /   _|| || ||_   \|_   _| | || |   .'    `.   | || |  |_   __ \   | || |  .'   '.     | || | |_   __ \    | || |   /  ___  |  | || | |  _   _  |  | || ||_   _||_   _|| || ||_  _| |_  _| | || ||_   _||_   _|| || | |_  _||_  _| | || | |_  _||_  _| | || |  |  __   _|  | || |   / _ __ `.  | |
>>> | |    / /\ \    | || |    | |_) |   | || |  / .'   \_|  | || |   | |   `. \ | || |   | |_  \_|  | || |   | |_  \_|  | || | / .'   \_|   | || |   | |__| |   | || |      | |     | || |      | |     | || |   | |_/ /    | || |    | |       | || |  |   \/   |  | || |  |   \ | |   | || |  /  .--.  \  | || |    | |__) |  | || | /  .-.  \    | || |   | |__) |   | || |  |  (__ \_|  | || | |_/ | | \_|  | || |  | |    | |  | || |  \ \   / /   | || |  | | /\ | |  | || |   \ \  / /   | || |   \ \  / /   | || |  |_/  / /    | || |  |_/____) |  | |
>>> | |   / ____ \   | || |    |  __'.   | || |  | |         | || |   | |    | | | || |   |  _|  _   | || |   |  _|      | || | | |    ____  | || |   |  __  |   | || |      | |     | || |   _  | |     | || |   |  __'.    | || |    | |   _   | || |  | |\  /| |  | || |  | |\ \| |   | || |  | |    | |  | || |    |  ___/   | || | | |   | |    | || |   |  __ /    | || |   '.___`-.   | || |     | |      | || |  | '    ' |  | || |   \ \ / /    | || |  | |/  \| |  | || |    > `' <    | || |    \ \/ /    | || |     .'.' _   | || |    /  ___.'  | |
>>> | | _/ /    \ \_ | || |   _| |__) |  | || |  \ `.___.'\  | || |  _| |___.' / | || |  _| |___/ |  | || |  _| |_       | || | \ `.___]  _| | || |  _| |  | |_  | || |     _| |_    | || |  | |_' |     | || |  _| |  \ \_  | || |   _| |__/ |  | || | _| |_\/_| |_ | || | _| |_\   |_  | || |  \  `--'  /  | || |   _| |_      | || | \  `-'  \_   | || |  _| |  \ \_  | || |  |`\____) |  | || |    _| |_     | || |   \ `--' /   | || |    \ ' /     | || |  |   /\   |  | || |  _/ /'`\ \_  | || |    _|  |_    | || |   _/ /__/ |  | || |    |_|       | |
>>> | ||____|  |____|| || |  |_______/   | || |   `._____.'  | || | |________.'  | || | |_________|  | || | |_____|      | || |  `._____.'   | || | |____||____| | || |    |_____|   | || |  `.___.'     | || | |____||____| | || |  |________|  | || ||_____||_____|| || ||_____|\____| | || |   `.____.'   | || |  |_____|     | || |  `.___.\__|  | || | |____| |___| | || |  |_______.'  | || |   |_____|    | || |    `.__.'    | || |     \_/      | || |  |__/  \__|  | || | |____||____| | || |   |______|   | || |  |________|  | || |    (_)       | |
>>> | |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | |
>>> | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
>>>  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------' 
OUPUT:
 .----------------.  .----------------.  .-----------------. .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .-----------------.
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
| | ____    ____ | || |      __      | || | ____  _____  | || |  ____  ____  | || |      __      | || |  _________   | || |  _________   | || |      __      | || | ____  _____  | |
| ||_   \  /   _|| || |     /  \     | || ||_   \|_   _| | || | |_   ||   _| | || |     /  \     | || | |  _   _  |  | || | |  _   _  |  | || |     /  \     | || ||_   \|_   _| | |
| |  |   \/   |  | || |    / /\ \    | || |  |   \ | |   | || |   | |__| |   | || |    / /\ \    | || | |_/ | | \_|  | || | |_/ | | \_|  | || |    / /\ \    | || |  |   \ | |   | |
| |  | |\  /| |  | || |   / ____ \   | || |  | |\ \| |   | || |   |  __  |   | || |   / ____ \   | || |     | |      | || |     | |      | || |   / ____ \   | || |  | |\ \| |   | |
| | _| |_\/_| |_ | || | _/ /    \ \_ | || | _| |_\   |_  | || |  _| |  | |_  | || | _/ /    \ \_ | || |    _| |_     | || |    _| |_     | || | _/ /    \ \_ | || | _| |_\   |_  | |
| ||_____||_____|| || ||____|  |____|| || ||_____|\____| | || | |____||____| | || ||____|  |____|| || |   |_____|    | || |   |_____|    | || ||____|  |____|| || ||_____|\____| | |
| |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------' 

Python 3: 145 bytes

i=input;l=int(i());h=int(i());t=[ord(c)-65if c.isalpha()else 26for c in i().upper()]
while h:h-=1;r=i();print(''.join(r[j*l:l*(j+1)] for j in t))

Try it online!

Dorian Turba

Posted 2019-12-23T16:55:51.580

Reputation: 263

2(j+1) can be -~j. The space between ] and for can be omitted. – Arnauld – 2019-12-23T17:27:54.397

1c.isalpha()*(ord(c)-91)+26 is shorter than the if. – manatwork – 2019-12-23T18:35:33.533

Is ? the only non-alphabetic character? – Jonathan Allan – 2019-12-23T18:38:44.297

1@JonathanAllan, that seems to be the joker to replace any non-alphabetic character, like in tr --complement 'A-Z' '?'. – manatwork – 2019-12-23T18:40:02.270

@JonathanAllan yes. If a character is non-alphabetic, it's ASCII art must be the '?' art. – Dorian Turba – 2019-12-23T21:21:37.630

@DorianTurba for one less byte total, we we can multiply by l inside the comprehension assigned to t (which is just l*, once we use an indexing lookup rather than an if-else) and use r[j-l:j] :) – Jonathan Allan – 2019-12-23T23:00:55.553

Yes, I see that in your answer. I remove the comment, thanks. – Dorian Turba – 2019-12-23T23:12:09.093

Is it allowed for the program to terminate with error after printing due to trying to read a line past the last line? – xnor – 2019-12-24T06:07:09.103

@xnor I don't know. You can try the code in the puzzle link provided in my question, the tests are in there. – Dorian Turba – 2019-12-24T06:09:47.933

Answers

2

Python 3,  131 130 128 126  125 bytes

i=input
L=int(i())
exec("t=[L*[27,ord(c)%32][c.isalpha()]for c in i()]"+";r=i();print(''.join(r[j-L:j]for j in t))"*int(i()))

A few golfs of the provided code.

Note: I upper-cased l to L and replaced a couple of ; with newlines just for legibility.

Try it online!

  1. When characters are alphabetical their ordinals are ranges \$[65,90]+[97,122]\$ which modulo \$32\$ are \$[1,27]+[1,27]=[1,27]\$. As such we can remove the .upper() and work with ord(c)%32 in this case.
  2. Rather than an if-else we can either use the fact that False is 0 and True is 1 and multiply (as originally suggested by manatwork in comments) or use list indexing for the two cases like above ([27,ord(c)%32][c.isalpha()] chooses the slice stop rather than its start).
  3. We can save a little by replacing the while loop with the exec of a string of code produced using string multiplication.
  4. We can save a byte by moving t=... into the exec-ed code so we do not have to assign to h at all. Another is saved by moving the repeated string's trailing ; to the beginning so that the t assignment string doesn't require its own.
  5. Rather than [(j-1)*L:j*L], or even [~-j*L:j*L], we can multiply by L once in the construction of t and just do [j-L:j].

Jonathan Allan

Posted 2019-12-23T16:55:51.580

Reputation: 67 804

The oneliner is just for the fun to have few bytes and lines, it's totally ok if it does not increase the number of bytes. – Dorian Turba – 2019-12-23T21:46:41.350

The explanation of your code is great, thank you a lot for this helpful answer. I'll wait a few days before accepting. – Dorian Turba – 2019-12-23T21:48:11.797