Skip to content

Instantly share code, notes, and snippets.

@mlovci
Created July 9, 2017 20:54
Show Gist options
  • Save mlovci/df54e92459be337dca7b61ebadb9c227 to your computer and use it in GitHub Desktop.
Save mlovci/df54e92459be337dca7b61ebadb9c227 to your computer and use it in GitHub Desktop.
A code poem.
def serpentine(n_max):
# \ /
# Y
# #
# #
# #
# #
# #
# ##
# #####
#""" #"""
## V V #
## T T #
## #
## * #
## =*= #
# # * #
# # #
b = -1
i = 1
j = 0
k = 0
while True:
if k <= 1:
i = 1
k += 1
elif b >= n_max-1:
if j == 1:
i = -1
j = 0
else:
j = 1
i = 0
elif b <= 0:
if j == 1:
i = 1
j = 0
else:
j = 1
i = 0
b += i
yield b
#####
####
###
##
#
@mlovci
Copy link
Author

mlovci commented Jul 9, 2017

s = serpentine(8)
[next(s) for _ in range(50)]
[0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 7,
 6,
 5,
 4,
 3,
 2,
 1,
 0,
 0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 7,
 6,
 5,
 4,
 3,
 2,
 1,
 0,
 0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 7,
 6,
 5,
 4,
 3,
 2,
 1,
 0,
 0,
 1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment