5
\$\begingroup\$

Regarding the Character picture exercise located at the end the following page: https://automatetheboringstuff.com/chapter4/

Say you have a list of lists where each value in the inner lists is a one-character string, like this:

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

You can think of grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin will be in the upper-left corner, the x-coordinates increase going right, and the y-coordinates increase going down.

Copy the previous grid value, and write code that uses it to print the image.

..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....

I wrote the following code, any feedback is appreciated.

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

for i in range(6):
    for a in range(9):
        if a < 8:
            print(grid[a][i], end="")
        else:
            print(grid[a][i])
\$\endgroup\$
4
\$\begingroup\$

Your code is pretty good. It's what I'd expect the author of the book to have wanted their readers to write.

Good job!


In terms of making the code more understandable/maintainable you can get the same functionality using zip, *args, ''.join and a single print.

for column in zip(*grid):
    print(''.join(column))
\$\endgroup\$
  • \$\begingroup\$ That's actually pretty cool! I had to think a moment to see what's going on here. But it's really clever. Basically a matrix transpose in plain Python. \$\endgroup\$ – AlexV Jun 14 at 15:16
  • \$\begingroup\$ Now that is short. Thanks for the answer. I will have to take a look at those functions. \$\endgroup\$ – drapozo Jun 14 at 20:45
3
\$\begingroup\$

As @Peilonrayz said - your code is well built and according to me, unexpectedly short which is certainly a good job as most people tend to write long programs for short tasks like this.


But you could make it much shorter (a single line perhaps) -

>>> print('\n'.join(map(''.join, zip(*grid))))

..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....

The zip(*grid) effectively transposes the matrix (flip it on the main diagonal), then each row is joined into one string, then the rows are joined with newlines so the whole thing can be printed at once.

Here's what map() in Python is -

The map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple, etc.)

Source - https://www.geeksforgeeks.org/python-map-function/

BUT

map() is a built-in function which focuses on functional programming. It may be useful for hacks and tricks since it shortens the code, but can be a bad idea for large projects in use.

Hope this helps!

\$\endgroup\$
  • \$\begingroup\$ Map's semi-depricated, defiantly discouraged. IIRC the Google code style also discourages it too. \$\endgroup\$ – Peilonrayz Jun 14 at 20:55
  • \$\begingroup\$ @Peilonrayz - I used map() because of this - stackoverflow.com/questions/1247486/list-comprehension-vs-map - for a more elegant solution. But I agree with your comment. \$\endgroup\$ – Justin Jun 15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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