Created
April 15, 2021 13:55
-
-
Save mesiriak/825f4255a92e813b9eb6bb0805533d72 to your computer and use it in GitHub Desktop.
Codewars. Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def spiralize(w): | |
arr = [[0 for _ in range(w)] for _ in range(w)] | |
for i in range(w): | |
arr[0][i] = 1 | |
x,y = w-1,1 | |
k = w-1 | |
arrow = (-1,1) | |
while k>0: | |
for i in range(k): | |
arr[y+arrow[1]*i][x] = 1 | |
y+=arrow[1]*(k-1) | |
if k == 1: | |
break | |
x-=arrow[1] | |
for i in range(k): | |
arr[y][x+arrow[0]*i] = 1 | |
x+=arrow[0]*(k-1) | |
y+=arrow[0] | |
k-=2 | |
arrow = (-arrow[0], -arrow[1]) | |
return arr | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment