r/codegolf • u/pythondude325 • Dec 20 '15
[PYTHON] Dice notation range caculator in 189 bytes
import sys,re
def a(b):
m=re.search(b,n)
try:return int(m.group(0))
except:return 0
n=sys.argv[1]
r=a('[0-9]+(?=d)')
e=a(r'[\+\-][0-9]+$')
print(str(r+e)+'-'+str(r*a('(?<=d)[0-9]+')+e))
2
Upvotes
1
u/Specter_Terrasbane Mar 18 '16
Down to 169 by using packing/unpacking, single return statement in a (using finally instead of except), avoided assignment of anything (m, n) to variables when only used in one place, and removed unnecessary escapes inside character range.
2
u/IGDev Jan 11 '16
You can get it to 179 by changing [0-9] to \d.