HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

ASCII generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
asciigeneratorstackoverflow

Problem

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 `20
11
MANHATTAN
.----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .----------

Solution

Generator expressions

for loops like this that are simply appending items to lists are a little ugly:

for i in xrange(h-1):
    output.append('')


This is not ideal. Rather, you can directly set the value of output like this:

output = ['' for _ in xrange(h - 1)]


The generator expression pattern can also be applied to this block of code as well:

for d in xrange(h):
    ascii_row = raw_input()
    ascii_rows.append(ascii_row)


The above code would the become this:

ascii_rows = [raw_input() for _ in xrange(h)]


Using str.join() to print lists

Rather than iterating over a list and printing each individual element like this:

for row in output:
    print row


You can just use str.join() and print the entire list with a certain separator character, like this:

print '\n'.join(output)


This eliminates the need for a loop at all.

Input validation

I know you're probably aware of input validation, and even though this is a trivial program, it's still good to have. This means that this block of code:

l = int(raw_input())
h = int(raw_input())


Should be wrapped in a try/except block that's catching a ValueError:

try:
    l = int(raw_input())
    h = int(raw_input())
except ValueError:
    ...


Nitpicks

There's not much to nitpick, but here we go:

-
If you want cross-compatibility with Python 3, you should use parentheses with print, like this:

print( ... )


-
l and h aren't very descriptive variable names. I'm assuming that they're synonymous with "length" and "height". I'd suggest changing them.

  • If you don't use a variable in a loop, then it should just name it _ rather than i, and such.

Code Snippets

for i in xrange(h-1):
    output.append('')
output = ['' for _ in xrange(h - 1)]
for d in xrange(h):
    ascii_row = raw_input()
    ascii_rows.append(ascii_row)
ascii_rows = [raw_input() for _ in xrange(h)]
for row in output:
    print row

Context

StackExchange Code Review Q#106876, answer score: 10

Revisions (0)

No revisions yet.