Skip to content

Instantly share code, notes, and snippets.

@heypoom
Created April 15, 2017 04:27
Show Gist options
  • Save heypoom/902182d9a954bb3ec9118b061abc0abe to your computer and use it in GitHub Desktop.
Save heypoom/902182d9a954bb3ec9118b061abc0abe to your computer and use it in GitHub Desktop.
(By Phoomparin) Some practice questions that asked you to print a wall -- in Python.
#!/usr/bin/python3
import sys
import math
# Figure out the width and height of our grid.
def getGridSize(boxes):
# For the walls (#)
# Exclude the center, times two for both sides, and add the center back.
walls = ((boxes - 1) * 2) + 1
return walls + (walls - 1)
def isBetween(min, max, row, col):
if (row >= min and row <= max) and (col >= min and col <= max):
return True
return False
# Figure out if this block should be a wall (#) or not (.)
def isWall(row, column, boxes):
grid = getGridSize(boxes)
middle = math.ceil(grid / 2) - 1
if (row == 1 or row == grid - 2) and (column > 0 and column < grid - 1):
# Row Walls
return False
elif (row > 1 and row < grid - 2) and (column == 1 or column == grid - 2):
# Column Walls
return False
elif (row >= 3 and row <= grid - 4) and (column >= 3 and column <= grid - 4):
if (row == middle and column == middle):
return True
# Loops through the minimum and maximum possible indexes
for i in reversed(range(4, 2 * (boxes - 1))):
# If it's in between the box range
if isBetween(i, grid - (i + 1), row, column):
# If it's an odd number, it's not a wall.
if (i % 2 != 0):
return False
# If it's an even number, make it a wall!
elif (i % 2 == 0):
return True
return False
return True
def renderBox(boxes):
grids = getGridSize(boxes)
print(boxes, "boxes,", grids, "grids")
for row in range(0, grids):
for column in range(0, grids):
if isWall(row, column, boxes):
print("#", end = "")
else:
print(".", end = "")
print("") # Insert newline for the new rows
renderBox(int(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment