repl.it
@anonymous/

SphericalPopularTeledu

Python 2.7

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys,re
V={}
S=lambda m:re.sub(' +',';',m).split(';')
def P(t):print t;return []
def E(t): 
 if len(t)==1:return t[0]
 y=E(t[1:])
 return{'print':'P('+y+')','sum':'sum('+y+')','product':'reduce(lambda x,y:x*y'+y+',1)'}[t[0]]
def e(t):
 y=t[0]
 if y=='let':v=V[t[1]]=e(t[3:]);return v
 if y=='print':v=e(t[1:]);return P(v)
 if y=='sum':return sum(e(t[1:]))
 if y=='product':return reduce(lambda x,y:x*y,e(t[1:]),1)
 if y=='for':c=S(R(t[1][1:-1].replace('/',' ')));return eval('map(lambda %s:%s,%s)' % (c[0],E(t[2:]),str(e(c[2:]))))
 if y[0]=='[':v=V['input']=eval(re.sub('([a-zA-Z]+)',"V['\\1']",y));return v
 if re.match('[0-9]+',y):v=V['input']=eval(y);return v
 return V[y]
def R(s, u='[',e=']',r=''):
 m,b='',1
 for c in s:
  if b:
   m+=c
   if c==u:b=0
  else:
   m+=r if c==' 'else c
   if c==e:b=1
 return m
def i(s):
 try:e(S(R(R(s.replace('^','**'),'(',')','/'))))
 except ZeroDivisionError:print 'Division by zero!';sys.exit(1)
while 1:print '>',;i(raw_input())