2
\$\begingroup\$

I understand there is another question on this already, however this is what I coded and wanted to know if this would raise any red flags?

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....

It's different in that it doesn't hard-code the length of the list.

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', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

x = 0
y = 0

for y in range(len(grid[x])):
    for x in range(len(grid)):
        print(grid[x][y],end='')
    print()

Thanks for the help and let me know if this is not appropriate to post!

\$\endgroup\$
0
3
\$\begingroup\$

It's wasteful to use range() and then only use the result for indexing.

Instead, just iterate directly over the values:

for col in range(len(grid[0])):
    for row in grid:
        print(row[col],end='')
    print()

We can simplify, though - use the zip function to transpose the array:

transposed = zip(*grid)

for row in transposed:
    for col in row:
        print(col,end='')
    print()

The next simplification is instead of printing one character at a time, we can use join to form strings for printing:

for row in zip(*grid):
    print(''.join(row))

And we can join all the rows using '\n'.join(), giving this final version:

print('\n'.join(''.join(x) for x in zip(*grid)))

Equivalently, using map:

print('\n'.join(map(''.join, zip(*grid))))
\$\endgroup\$
2
  • \$\begingroup\$ This is great, thanks @toby. Can I ask why using range here is wasteful? the idea was to build a code that can work with any number of rows/columns. I got to learn a lot here, thanks! \$\endgroup\$ May 25 at 5:20
  • 1
    \$\begingroup\$ Perhaps "wasteful" isn't exactly the right word. My point is that we don't need to count the columns and create indexes when it's simpler to iterate the columns directly. \$\endgroup\$ May 25 at 7:02

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.