Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 3 10 5 16 8 4 2 1
import sys
def collatz(n):
out = str(n)
while n > 1:
if n % 2 == 1:
n = n*3 + 1
else:
n = n / 2
out += ' ' + str(n)
return out
def to_string_literal(s):
return s.replace('\\', '\\\\').replace('\n','\\n').replace('"', '\\"')
data = "# COLLATZ\nimport sys\n\ndef collatz(n):\n out = str(n)\n while n > 1:\n if n % 2 == 1:\n n = n*3 + 1\n else:\n n = n / 2\n out += ' ' + str(n)\n return out\n\ndef to_string_literal(s):\n return s.replace('\\\\', '\\\\\\\\').replace('\\n','\\\\n').replace('\"', '\\\\\"')\n\ndata = \"DATA\"\n\nstart = int(sys.argv[1])\ndata = data.replace('DATA', to_string_literal(data), 1)\ndata = data.replace('COLLATZ', collatz(start), 1)\nprint data"
start = int(sys.argv[1])
data = data.replace('DATA', to_string_literal(data), 1)
data = data.replace('COLLATZ', collatz(start), 1)
print data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.