Skip to content

Instantly share code, notes, and snippets.

@batemapf
Created February 5, 2018 18:58
Show Gist options
  • Save batemapf/b06d3165636397a1bd4350a1d9ebf024 to your computer and use it in GitHub Desktop.
Save batemapf/b06d3165636397a1bd4350a1d9ebf024 to your computer and use it in GitHub Desktop.
# Let's say we wanted to print an array of the strings "p" and "q" that is
# 10 wide by 10 tall that looks like this:
"""
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
p q q q q q q q q q
"""
# One way to do is by using the following strategy.
# First, set the max size of width and height.
max_size = 10
# Then, set the starting point, which is the top left-most corner of the array.
row = 1
col = 1
# Do "something" so long as the the number of rows (the height of the array)
# is less than or equal to the maximum height specified.
while row <= max_size:
# That "something", in this case, is print the string "p" followed by a
# space. Then...
print('p', end=' ')
# Start a another loop inside of the first loop (see line 27) that does
# "another thing" so long as the number of columns is less than the maximum
# width specified.
# *Note* that we are using "less than" rather than less than or equal to.
# This is because the first column of this particular row was already
# printed by the first loop.
while col < max_size:
# That "another thing", in this case, is print the string "q" followed
# by a space.
print('q', end=' ')
# Then we increment the column number which is fed back into the
# second while loop on line 40. This will continue to increment
# until the the maximum width of the array is reached.
col = col + 1
# Once the maximum width for the row is reached, print an empty string
# which will create a new line.
print('')
# Reset the column number to 1 because we that is where we want to start
# printing the "q"s in the next row.
col = 1
# Increment the row number which will be fed back into the first while
# loop on line 27.
row = row + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment