RosettaCodeData/Task/Cantor-set/Python/cantor-set-1.py

28 lines
642 B
Python
Raw Permalink Normal View History

2026-02-01 16:33:20 -08:00
from __future__ import print_function, division
2023-07-01 11:58:00 -04:00
WIDTH = 81
HEIGHT = 5
2026-02-01 16:33:20 -08:00
lines = []
2023-07-01 11:58:00 -04:00
def cantor(start, len, index):
2026-02-01 16:33:20 -08:00
seg = len // 3 # from division
2023-07-01 11:58:00 -04:00
if seg == 0:
return None
2026-02-01 16:33:20 -08:00
for it in range(HEIGHT - index):
2023-07-01 11:58:00 -04:00
i = index + it
2026-02-01 16:33:20 -08:00
for jt in range(seg):
2023-07-01 11:58:00 -04:00
j = start + seg + jt
pos = i * WIDTH + j
lines[pos] = ' '
cantor(start, seg, index + 1)
cantor(start + seg * 2, seg, index + 1)
return None
2026-02-01 16:33:20 -08:00
lines = ['*'] * (WIDTH * HEIGHT)
2023-07-01 11:58:00 -04:00
cantor(0, WIDTH, 1)
2026-02-01 16:33:20 -08:00
for i in range(HEIGHT):
2023-07-01 11:58:00 -04:00
beg = WIDTH * i
2026-02-01 16:33:20 -08:00
print(''.join(lines[beg : beg + WIDTH])) # from print_function