Skip to content

Instantly share code, notes, and snippets.

@swanandp
Created March 27, 2024 06:56
Show Gist options
  • Save swanandp/ebf62953b7319468d18c448d2a8f393f to your computer and use it in GitHub Desktop.
Save swanandp/ebf62953b7319468d18c448d2a8f393f to your computer and use it in GitHub Desktop.
Python code to generate the Hindu symbol Swastika of any given size
# generate a स्वस्तिक (swastika) of arm size n
# for n = 3
# * * * * *
# * *
# * *
# * * * * * * *
# * *
# * *
# * * * * *
# For easily identifying the directions:
# "right": (0, 1), # same row, next column
# "up": (-1, 0), # previous row, same column
# "left": (0, -1), # same row, previous column
# "down": (1, 0), # next row, same column
def turn_right(current):
return {
(0, 1): (1, 0), # right from right is down
(-1, 0): (0, 1), # right from up is right
(0, -1): (-1, 0), # right from left is up
(1, 0): (0, -1), # right from down is left
}[current]
def swastika(size: int):
m = [[" " for _ in range(2 * size + 1)] for _ in range(2 * size + 1)]
if size < 0:
return m
m[size][size] = "*"
directions = [(0, 1), (-1, 0), (0, -1), (1, 0)] # right, up, left, down
# get the deltas for: starting direction, then turn right direction
for arm in [[d] * size + [turn_right(d)] * size for d in directions]:
x0, y0 = size, size
for dx, dy in arm:
x0 += dx
y0 += dy
m[x0][y0] = "*"
return m
def print_swastika(m):
for row in m:
print(" ".join(row))
swastika3 = """
* * * * *
* *
* *
* * * * * * *
* *
* *
* * * * *
""".strip().split("\n")
swastika2 = """
* * * *
* *
* * * * *
* *
* * * *
""".strip().split("\n")
assert [" ".join(line) for line in swastika(3)] == swastika3
assert [" ".join(line) for line in swastika(2)] == swastika2
print_swastika(swastika(3))
print_swastika(swastika(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment