Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_array(array):
- for i in range(0, len(array)):
- tmp = [array[i][j] for j in range(0, len(array[i]))]
- print(*tmp)
- class Filler():
- def __init__(self, size):
- self.number = 1
- self.size = size
- self.direction = 1
- self.position = {"x": -1, "y": 0}
- def next(self, array):
- if self.number > self.size ** 2:
- return False
- new_position = self.next_position()
- while not (0 <= new_position["y"] < self.size and 0 <= new_position["x"] < self.size
- and array[new_position["y"]][new_position["x"]] is False):
- self.change_direction()
- new_position = self.next_position()
- self.position = new_position
- array[new_position["y"]][new_position["x"]] = self.number
- self.number += 1
- # print_array(array)
- return True
- def change_direction(self):
- self.direction = self.direction + 1 if self.direction < 3 else 0
- return self.direction
- def next_position(self):
- if self.direction == 0:
- return {
- "x": self.position["x"],
- "y": self.position["y"] - 1,
- }
- elif self.direction == 1:
- return {
- "x": self.position["x"] + 1,
- "y": self.position["y"],
- }
- elif self.direction == 2:
- return {
- "x": self.position["x"],
- "y": self.position["y"] + 1,
- }
- elif self.direction == 3:
- return {
- "x": self.position["x"] - 1,
- "y": self.position["y"],
- }
- size = int(input())
- array = [[False for j in range(0, size)] for i in range(0, size)]
- filler = Filler(size)
- while filler.next(array):
- pass
- print_array(array)
Advertisement
Add Comment
Please, Sign In to add comment