Skip to content

Instantly share code, notes, and snippets.

@jorgebastida
Created June 13, 2011 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jorgebastida/1023399 to your computer and use it in GitHub Desktop.
Save jorgebastida/1023399 to your computer and use it in GitHub Desktop.
Tuenti ejercicio 1
import re
OPERADORES = {'^=': lambda x,y: x+y,
'^@': lambda x,y: x-y,
'^#': lambda x,y: x*y}
OP_RE = r'(\^\=|\^\@|\^\#) (\-?\d+)( \-?\d+)?\$'
def componer(op):
return '%s$' % ' '.join(op)
def operar(op):
if len(op) == 2:
return str(int(op[1]) * -1)
return str(OPERADORES.get(op[0])(*map(int, op[1:])))
def main(input):
for linea in lineas.split('\n'):
remaining = True
while remaining:
match = re.search(OP_RE, linea)
op = [m.strip() for m in match.groups() if m]
linea = linea.replace(componer(op), operar(op))
try:
resultado = int(linea)
remaining = False
except:
pass
print resultado
lineas = """^= ^# ^# 75 208$ ^@ 86$$ 123$
^@ ^# 105 ^# 227 205$$$
^= 84 ^# 273 125$$
^# ^= 10 ^# 256 28$$ 162$
^= ^@ 101 4$ ^@ 268 43$$"""
if __name__ == '__main__':
main(lineas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment