September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,12 +1,41 @@
def spiral_matrix(n):
m = [[0] * n for i in range(n)]
dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]
x, y, c = 0, -1, 1
for i in range(n + n - 1):
for j in range((n + n - i) // 2):
x += dx[i % 4]
y += dy[i % 4]
m[x][y] = c
c += 1
return m
for i in spiral_matrix(5): print(*i)
'''Spiral Matrix'''
# spiral :: Int -> [[Int]]
def spiral(n):
'''The rows of a spiral matrix of order N.
'''
def go(rows, cols, x):
return [list(range(x, x + cols))] + [
list(reversed(x)) for x
in zip(*go(cols, rows - 1, x + cols))
] if 0 < rows else [[]]
return go(n, n, 0)
# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Spiral matrix of order 5, in wiki table markup'''
print(wikiTable(
spiral(5)
))
# FORMATTING ----------------------------------------------
# wikiTable :: [[a]] -> String
def wikiTable(rows):
'''Wiki markup for a no-frills tabulation of rows.'''
return '{| class="wikitable" style="' + (
'width:12em;height:12em;table-layout:fixed;"|-\n'
) + '\n|-\n'.join([
'| ' + ' || '.join([str(cell) for cell in row])
for row in rows
]) + '\n|}'
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -1,5 +1,12 @@
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
def spiral_matrix(n):
m = [[0] * n for i in range(n)]
dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]
x, y, c = 0, -1, 1
for i in range(n + n - 1):
for j in range((n + n - i) // 2):
x += dx[i % 4]
y += dy[i % 4]
m[x][y] = c
c += 1
return m
for i in spiral_matrix(5): print(*i)

View file

@ -0,0 +1,5 @@
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9