-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractal_computer.py
55 lines (53 loc) · 1.78 KB
/
fractal_computer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def set_slash(drawing: list[list[str]], x: int, y: int, sens: bool, toggle: bool) -> bool:
if toggle:
drawing[y][x] = '/' if sens else '\\'
return not toggle
def compute(width: int, height: int) -> list[list[str]]:
drawing = [['*' for _ in range(width)] for _ in range(height)]
x = 0
y = 0
x_direction = 1
y_direction = 1
toggle = True
sens = False
x_flag = True
y_flag = True
while True:
toggle = set_slash(drawing, x, y, sens, toggle)
if ((x == width - 1 and y == height - 1) or (x == 0 and y == 0) or (x == width - 1 and y == 0) or (
x == 0 and y == height - 1)) and not x_flag and not y_flag:
break
elif x == width - 1 and not x_flag:
y += y_direction
x_direction = -1
sens = not sens
toggle = set_slash(drawing, x, y, sens, toggle)
toggle = not toggle
x_flag = True
elif x == 0 and not x_flag:
y += y_direction
x_direction = 1
sens = not sens
toggle = set_slash(drawing, x, y, sens, toggle)
toggle = not toggle
x_flag = True
elif y == height - 1 and not y_flag:
x += x_direction
y_direction = -1
sens = not sens
toggle = set_slash(drawing, x, y, sens, toggle)
toggle = not toggle
y_flag = True
elif y == 0 and not y_flag:
x += x_direction
y_direction = 1
sens = not sens
toggle = set_slash(drawing, x, y, sens, toggle)
toggle = not toggle
y_flag = True
else:
x += x_direction
y += y_direction
x_flag = False
y_flag = False
return drawing