Ask your own question, for FREE!
MIT 6.01SC Introduction to Electrical Engineering and Computer Science I 19 Online
OpenStudy (anonymous):

Homework 1: I didn't complete the either of the extension problems, because I felt that if you make it through the main assignment, the extensions would be simple.

OpenStudy (anonymous):

## Tokenizer: # Convert strings into a list of tokens (strings) def tokenize(string): list_test = string.split() #print "list :", list_test tokens=[] for item in list_test: temp ="" #print "item :", item for ind in range(len(item)): if temp == "" and ind == 0 and item[ind] in seps: tokens.append( item[ind]) #print "1 :", tokens elif temp == "" and ind > 0 and item[ind] in seps: if item[ind-1] not in seps: tokens.append(item[:ind]) #print "2 :", tokens tokens.append(item[ind]) #print "3 :", tokens elif temp != "" and ind > 0 and item[ind] in seps: tokens.append(temp) temp = "" #print "n4 :", tokens tokens.append(item[ind]) #print "n5 :", tokens else: temp = temp + item[ind] #print "temp :", temp if ind + 1 == len(item): tokens.append(temp) #print "4 :", tokens return tokens

OpenStudy (anonymous):

## Parser: # tokens is a list of tokens # returns a syntax tree: an instance of {\tt Number}, {\tt Variable}, # or one of the subclasses of {\tt BinaryOp} def parse(tokens): def parseExp(index): if tokens[index] == "(": leftTree = parseExp(index +1) rightTree = parseExp(leftTree[-1] +1) if tokens[leftTree[-1]] == "+": return (Sum(leftTree[0],rightTree[0]), rightTree[-1] +1) elif tokens[leftTree[-1]] == "-": return (Diff(leftTree[0],rightTree[0]), rightTree[-1] +1) elif tokens[leftTree[-1]] == "*": return (Prod(leftTree[0],rightTree[0]), rightTree[-1] +1) elif tokens[leftTree[-1]] == "/": return (Quot(leftTree[0],rightTree[0]), rightTree[-1] +1) elif tokens[leftTree[-1]] == "=": return (Assign(leftTree[0],rightTree[0]), rightTree[-1] +1) elif numberTok(tokens[index]): return (Number(tokens[index]), index + 1) elif variableTok(tokens[index]): return (Variable(tokens[index]), index +1) elif tokens[index] == ")": print "WHY HERE" pass (parsedExp, nextIndex) = parseExp(0) return parsedExp

OpenStudy (anonymous):

## Evaluation Statements ## This is far to long to post here, I will post one and you should be able to recreate the rest of them: class Sum(BinaryOp): opStr = 'Sum' def eval(self,env): if self.left.__class__.__name__ == "Variable": left = env[self.left.name] elif self.left.__class__.__name__ == "Number": left = self.left.value else: left = self.left.eval(env) if self.right.__class__.__name__ == "Variable": right = env[self.right.name] elif self.right.__class__.__name__ == "Number": right = self.right.value else: right = self.right.eval(env) return left + right

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!