Created
June 13, 2011 18:41
-
-
Save jorgebastida/1023399 to your computer and use it in GitHub Desktop.
Tuenti ejercicio 1
This file contains 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
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