Created
April 12, 2016 20:57
-
-
Save misterhat/85b946fff5c9aa68467139913fdb5432 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
#!/usr/bin/env python3 | |
# https://codegolf.stackexchange.com/questions/57891/curly-numbers | |
import re | |
import math | |
CURLIES = [ | |
"{;;}", # 8 | |
"{{;};};", # 21 | |
"{};;;", # 3 | |
"{{{{;}}};}", # 260 | |
"{}", # 0 | |
"{};;;;;", # 5 | |
"{{;;;;};;}", # 72 | |
"c{u;r;l}y;!", # 9 | |
"42{;} ;;;;" # 8 | |
] | |
NUMBERS = [ | |
4, | |
17, | |
1, | |
0, | |
96, | |
100, | |
112, | |
123 | |
] | |
def un_curl(curled): | |
curled = ''.join(re.findall('[{};]', curled)) | |
# store the previous character | |
last = None | |
# final result | |
num = 0 | |
# stack for braces | |
stack = 0 | |
if curled[0] != '{': | |
return False | |
for c in curled: | |
if c == '{': | |
# braces cannot be touching, and semicolon can't be next to group | |
if last == '}' or last == ';': | |
return False | |
stack += 1 | |
elif c == '}': | |
num *= 4 | |
stack -= 1 | |
elif c == ';': | |
num += 1 | |
last = c | |
# make sure all of the braces have been closed | |
return num if stack == 0 else False | |
def curl(num): | |
curled = '' | |
braces = 0 | |
# fuck you | |
if num == 1: | |
return '{};' | |
elif num == 0: | |
return '{}' | |
while num > 0: | |
curled = (num % 4 * ';') + curled | |
if num > 1: | |
curled = '}' + curled | |
braces += 1 | |
num = math.floor(num / 4) | |
return (braces * '{') + curled | |
[print(curly, un_curl(curly)) for curly in CURLIES] | |
[print(num, curl(num)) for num in NUMBERS] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment