Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ltype = type([])
- class NonCallableError(Exception):
- def __init__(self,st):
- self.st = st
- def __str__(self):
- return self.st + " is not a callable object."
- def filter(conditional,xs):
- return [x for x in xs if conditional(x)]
- def map(function,xs):
- return [function(x) for x in xs]
- def reduce(funtction,xs,start):
- for x in xs:
- start = function(start,x)
- return start
- def is_number(s):
- try:
- float(s)
- return True
- except ValueError:
- return False
- def s_parse(s,index=0):
- xs = []
- inquotes = False
- escaped = False
- word = ""
- while index < len(s):
- if s[index] == "(" and not inquotes:
- inner,index = s_parse(s,index+1)
- xs.append(inner)
- elif s[index] == '"' and not escaped and not inquotes:
- inquotes = True
- word = '"'
- elif s[index] == '"' and not escaped and inquotes:
- inquotes = False
- word += '"'
- elif s[index] == " " and not inquotes:
- xs.append(word)
- word = ""
- elif s[index] == ")" and not inquotes:
- xs.append(word)
- xs = filter(lambda word: word != "",xs)
- return xs,index
- else:
- word += s[index]
- index += 1
- return xs[0]
- print(s_parse('(1 2 "stenrasotoin oarnt ()()(" (3 4 5 (6)) 7)'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement