Skip to content

Instantly share code, notes, and snippets.

@anonymuse
Last active June 24, 2022 11:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymuse/c5505b02ac2c5ef05b14 to your computer and use it in GitHub Desktop.
Save anonymuse/c5505b02ac2c5ef05b14 to your computer and use it in GitHub Desktop.
Automate the Boring Stuff with Python -- Chapter 04 -- Character Picture Grid

Character Picture Grid Problem

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

Hint: You will need to use a loop in a loop in order to print grid[0][0], then grid[1][0], then grid[2][0], and so on, up to grid[8][0]. This will finish the first row, so then print a newline. Then your program should print grid[0][1], then grid[1][1], then grid[2][1], and so on. The last thing your program will print is grid[8][5].

Also, remember to pass the end keyword argument to print() if you don’t want a newline printed automatically after each print() call.

'''
SPOILER ALERT! SPOILER ALERT!
Solution ahead
SPOILER ALERT! SPOILER ALERT!
```
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', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
def dot_matrix(ribbon):
n = 0
while n < (len(grid[0])):
for i in range(len(grid)):
print grid[i][n],
print ''
n += 1
dot_matrix(grid)
@mKotoulas
Copy link

mKotoulas commented Jun 24, 2018

Thats my simple solution. I think its not that big.

screenshot_1

@DannyMexen
Copy link

DannyMexen commented Nov 14, 2018

This is my solution

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

print()`

But the problem I have with this is that how do you handle situations in which the lists within grid[x] vary in length? Sure, it is easy to pick any index here like I have grid[0] because each one is equal in length, but what if grid[1] only had len = 5 or 4. How do you solve this?

@surigangula
Copy link

surigangula commented Dec 23, 2018

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

#first figure out what we need to print: [0][0], [1][0], [2][0]..............[upto number of rows][0]
#then we need to print similarly but this time x becomes [1]
#end will make it print the next char, same line
#print("") move to next line to print

@megamillions
Copy link

You can also use reversed() in this exercise:

grid = [['.', '.', '.', '.', '.', '.'],		# x0
        ['.', 'O', 'O', '.', '.', '.'],		# x1
        ['O', 'O', 'O', 'O', '.', '.'],		# x2
        ['O', 'O', 'O', 'O', 'O', '.'],		# x3
        ['.', 'O', 'O', 'O', 'O', 'O'],		# x4
        ['O', 'O', 'O', 'O', 'O', '.'],		# x5
        ['O', 'O', 'O', 'O', '.', '.'],		# x6
        ['.', 'O', 'O', '.', '.', '.'],		# x7
        ['.', '.', '.', '.', '.', '.']]		# x8
		# y0, y1, y2, y3, y4, y5

heart = ''

# j will become the future y
for j in range(len(grid[-1])):

	# i will become the future x
	for i in reversed(range(len(grid))):

		heart += grid[i][j]

		if i == 0:
		
			heart += '\n'

print(heart)

@CappeArg
Copy link

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

for y in range(0,len(grid[0])):
for x in range(0,len(grid)):
if x==8:
print(grid[x][y])
break
print(grid[x][y], end="")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment