Today I stumbled upon CodinGame, a site with programming challenges. I'll describe one of them.
ASCII art allows you to represent forms by using characters. To be precise, in our case, these forms are words. For example, the word "MANHATTAN" could be displayed as follows in ASCII art:
# # # ### # # # ### ### # ### ### # # # # # # # # # # # # # # ### ### # # ### ### # # ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Your mission is to write a program that can display a line of text in ASCII art.
INPUT:
Line 1: the width L of a letter represented in ASCII art. All letters are the same width.
Line 2: the height H of a letter represented in ASCII art. All letters are the same height.
Line 3: The line of text T, composed of N ASCII characters. Following Lines: the string of characters ABCDEFGHIJKLMNOPQRSTUVWXYZ? Represented in ASCII art.OUTPUT:
The text T in ASCII art.
The characters a to z are shown in ASCII art by their equivalent in upper case.
The characters which are not in the intervals [a-z] or [A-Z] will be shown as a question mark in ASCII art.CONSTRAINTS :
0 < L < 30
0 < H < 30
0 < N < 200
Example input:
20
11
MANHATTAN
.----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
| | __ | || | ______ | || | ______ | || | ________ | || | _________ | || | _________ | || | ______ | || | ____ ____ | || | _____ | || | _____ | || | ___ ____ | || | _____ | || | ____ ____ | || | ____ _____ | || | ____ | || | ______ | || | ___ | || | _______ | || | _______ | || | _________ | || | _____ _____ | || | ____ ____ | || | _____ _____ | || | ____ ____ | || | ____ ____ | || | ________ | || | ______ | |
| | / \ | || | |_ _ \ | || | .' ___ | | || | |_ ___ `. | || | |_ ___ | | || | |_ ___ | | || | .' ___ | | || | |_ || _| | || | |_ _| | || | |_ _| | || | |_ ||_ _| | || | |_ _| | || ||_ \ / _|| || ||_ \|_ _| | || | .' `. | || | |_ __ \ | || | .' '. | || | |_ __ \ | || | / ___ | | || | | _ _ | | || ||_ _||_ _|| || ||_ _| |_ _| | || ||_ _||_ _|| || | |_ _||_ _| | || | |_ _||_ _| | || | | __ _| | || | / _ __ `. | |
| | / /\ \ | || | | |_) | | || | / .' \_| | || | | | `. \ | || | | |_ \_| | || | | |_ \_| | || | / .' \_| | || | | |__| | | || | | | | || | | | | || | | |_/ / | || | | | | || | | \/ | | || | | \ | | | || | / .--. \ | || | | |__) | | || | / .-. \ | || | | |__) | | || | | (__ \_| | || | |_/ | | \_| | || | | | | | | || | \ \ / / | || | | | /\ | | | || | \ \ / / | || | \ \ / / | || | |_/ / / | || | |_/____) | | |
| | / ____ \ | || | | __'. | || | | | | || | | | | | | || | | _| _ | || | | _| | || | | | ____ | || | | __ | | || | | | | || | _ | | | || | | __'. | || | | | _ | || | | |\ /| | | || | | |\ \| | | || | | | | | | || | | ___/ | || | | | | | | || | | __ / | || | '.___`-. | || | | | | || | | ' ' | | || | \ \ / / | || | | |/ \| | | || | > `' < | || | \ \/ / | || | .'.' _ | || | / ___.' | |
| | _/ / \ \_ | || | _| |__) | | || | \ `.___.'\ | || | _| |___.' / | || | _| |___/ | | || | _| |_ | || | \ `.___] _| | || | _| | | |_ | || | _| |_ | || | | |_' | | || | _| | \ \_ | || | _| |__/ | | || | _| |_\/_| |_ | || | _| |_\ |_ | || | \ `--' / | || | _| |_ | || | \ `-' \_ | || | _| | \ \_ | || | |`\____) | | || | _| |_ | || | \ `--' / | || | \ ' / | || | | /\ | | || | _/ /'`\ \_ | || | _| |_ | || | _/ /__/ | | || | |_| | |
| ||____| |____|| || | |_______/ | || | `._____.' | || | |________.' | || | |_________| | || | |_____| | || | `._____.' | || | |____||____| | || | |_____| | || | `.___.' | || | |____||____| | || | |________| | || ||_____||_____|| || ||_____|\____| | || | `.____.' | || | |_____| | || | `.___.\__| | || | |____| |___| | || | |_______.' | || | |_____| | || | `.__.' | || | \_/ | || | |__/ \__| | || | |____||____| | || | |______| | || | |________| | || | (_) | |
| | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | || | | |
| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
Example output:
.----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .-----------------.
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
| | ____ ____ | || | __ | || | ____ _____ | || | ____ ____ | || | __ | || | _________ | || | _________ | || | __ | || | ____ _____ | |
| ||_ \ / _|| || | / \ | || ||_ \|_ _| | || | |_ || _| | || | / \ | || | | _ _ | | || | | _ _ | | || | / \ | || ||_ \|_ _| | |
| | | \/ | | || | / /\ \ | || | | \ | | | || | | |__| | | || | / /\ \ | || | |_/ | | \_| | || | |_/ | | \_| | || | / /\ \ | || | | \ | | | |
| | | |\ /| | | || | / ____ \ | || | | |\ \| | | || | | __ | | || | / ____ \ | || | | | | || | | | | || | / ____ \ | || | | |\ \| | | |
| | _| |_\/_| |_ | || | _/ / \ \_ | || | _| |_\ |_ | || | _| | | |_ | || | _/ / \ \_ | || | _| |_ | || | _| |_ | || | _/ / \ \_ | || | _| |_\ |_ | |
| ||_____||_____|| || ||____| |____|| || ||_____|\____| | || | |____||____| | || ||____| |____|| || | |_____| | || | |_____| | || ||____| |____|| || ||_____|\____| | |
| | | || | | || | | || | | || | | || | | || | | || | | || | | |
| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
It's a programming-challenge, so the usual input validation, argument handling and over engineering was skipped. My code scores 100% (which only means it's bug free for all cases ran), but it feels like it could've been more succinct.
There's a lot of looping going around, which is probably a bad thing. And the naming probably could've been better. I didn't adhere strictly to the naming given by the assignment, since the program checking the code doesn't care anyway.
l = int(raw_input())
h = int(raw_input())
text_source = raw_input()
ascii_rows = []
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ?'
for d in xrange(h):
ascii_row = raw_input()
ascii_rows.append(ascii_row)
output = ['']
for i in xrange(h-1):
output.append('')
for character in text_source:
found = False
for ascii in xrange(len(alphabet)):
if character.upper() == alphabet[ascii]:
found = True
for it in xrange(h):
output[it] += ascii_rows[it][l * ascii:l * (ascii + 1)]
if not found:
for it in xrange(h):
output[it] += ascii_rows[it][l * ascii:l * (ascii + 1)]
for row in output:
print row