Created
July 5, 2014 03:57
-
-
Save graue/0e8242f24608ac8969f0 to your computer and use it in GitHub Desktop.
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
# 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