Skip to content

Instantly share code, notes, and snippets.

@scottrogowski
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save scottrogowski/987aca30a5ed783a0945 to your computer and use it in GitHub Desktop.

Select an option

Save scottrogowski/987aca30a5ed783a0945 to your computer and use it in GitHub Desktop.
Single expression one line python spiral
# This prints a spiral onto the terminal.
# It is composed of a few list comprehensions, a few ternary expressions, and a y-combinator
# The only python keywords used: print, join, in, lambda, if, else, int, and, or, range
# No libraries are used
# It is 308 characters although I think it would be possible to get it under 300
# with some further optimization of the spiral generator. Particularly I think
# it might be possible to build this without storing state recursively at the
# expense of some additional computation. This could be done by finding an
# expression that would map the element index of the spiral to the coordinates
# of that element
print ''.join([((i, j) in (lambda r:lambda p,z,m,f:r(r,p,z,m,f))(lambda r, p, z, m,f: [f(p, z)] + r(r, f(p, z), z+1, m, f) if z!=m else [])((20,20),0,300, lambda p, z: (p[0] + (int(z**.5)%2 and (-1)**(int(z**.5)/2)), p[1] + ((int(z**.5)+1)%2 and (-1)**(int(z**.5)/2))))) and '0' or j and ' ' or '\n ' for i in range(40) for j in range(40)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment