Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IvanFrecia/942db8cf65b9db0d6aae9427c6f4d856 to your computer and use it in GitHub Desktop.
Save IvanFrecia/942db8cf65b9db0d6aae9427c6f4d856 to your computer and use it in GitHub Desktop.
Class Variables and Instance Variables
class Point:
""" Point class for representing and manipulating x,y coordinates. """
printed_rep = "*"
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def graph(self):
rows = []
size = max(int(self.x), int(self.y)) + 2
for j in range(size-1) :
if (j+1) == int(self.y):
special_row = str((j+1) % 10) + (" "*(int(self.x) -1)) + self.printed_rep
rows.append(special_row)
else:
rows.append(str((j+1) % 10))
rows.reverse() # put higher values of y first
x_axis = ""
for i in range(size):
x_axis += str(i % 10)
rows.append(x_axis)
return "\n".join(rows)
p1 = Point(2, 3)
p2 = Point(3, 12)
print(p1.graph())
print()
print(p2.graph())
# Output:
# 4
# 3 *
# 2
# 1
# 01234
# 3
# 2 *
# 1
# 0
# 9
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1
# 01234567890123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment