Skip to content

Instantly share code, notes, and snippets.

@jbourassa
Created June 3, 2012 20:59
Show Gist options
  • Save jbourassa/2865033 to your computer and use it in GitHub Desktop.
Save jbourassa/2865033 to your computer and use it in GitHub Desktop.
Udacity Exam - 6 tests
example4 = [ \
("assign", "x", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "y", ("binop", ("identifier","b"), "+", ("identifier","c"))) ,
("assign", "z", ("binop", ("identifier","c"), "+", ("identifier","d"))) ,
("assign", "b", ("binop", ("identifier","c"), "+", ("identifier","d"))) ,
("assign", "z", ("number", 5)) ,
("assign", "p", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "q", ("binop", ("identifier","b"), "+", ("identifier","c"))) ,
("assign", "r", ("binop", ("identifier","c"), "+", ("identifier","d"))) ,
]
answer4 = [ \
("assign", "x", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "y", ("binop", ("identifier","b"), "+", ("identifier","c"))) ,
("assign", "z", ("binop", ("identifier","c"), "+", ("identifier","d"))) ,
("assign", "b", ("identifier", "z")) ,
("assign", "z", ("number", 5)) ,
("assign", "p", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "q", ("binop", ("identifier","b"), "+", ("identifier","c"))) ,
("assign", "r", ("identifier", "b")) ,
]
print optimize(example4) == answer4
example4a = [\
("assign", "x", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "y", ("binop", ("identifier","x"), "+", ("identifier","c"))) ,
("assign", "z", ("binop", ("binop", ("identifier","a"), "+", ("identifier","b")), "+", ("identifier","c"))) ,
]
answer4b = [\
("assign", "x", ("binop", ("identifier","a"), "+", ("identifier","b"))) ,
("assign", "y", ("binop", ("identifier","x"), "+", ("identifier","c"))) ,
("assign", "z", ("identifier", "y")) ,
]
print optimize(example4a) == answer4b
# x = a + e + b + c;
# e = 2;
# z = a + e + b + c;
#
example4c = [ \
('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'e', ("number", 2)),
('assign', 'z', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
]
answer4c = [ \
('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'e', ("number", 2)),
('assign', 'z', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c')))
]
print optimize(example4c) == answer4c
# x = a + e + b + c;
# y = e;
# z = a + e + b + c;
#
example4c = [ \
('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'y', ("identifier", 'e')),
('assign', 'z', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
]
answer4c = [ \
('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'y', ("identifier", 'e')),
('assign', 'z', ("identifier", 'x'))
]
print optimize(example4c) == answer4c
# x = a + b + c;
# y = 2;
# z = a + b + c;
#
#
example1 = [('assign', 'x', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'y', ("number", 2)),
('assign', 'z', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'b')), '+', ('identifier', 'c')))]
answer1 = [('assign', 'x', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'y', ("number", 2)),
('assign', 'z', ('identifier', 'x'))]
print optimize(example1) == answer1
# x = a + e + b + c;
# e = 2;
# z = a + e + b + c;
#
example2 = [('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'e', ("number", 2)),
('assign', 'z', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c')))]
answer2 = [('assign', 'x', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c'))),
('assign', 'e', ("number", 2)),
('assign', 'z', ('binop', ('binop', ('binop', ('identifier', 'a'), '+', ('identifier', 'e')), '+', ('identifier', 'b')), '+', ('identifier', 'c')))]
print optimize(example2) == answer2
# self-assign
# x = x + z + 1;
# y = x + z + 1;
#
#
example3 = [ \
("assign", "x", ("binop", ("identifier","x"), "+", ("binop", ("identifier","z"), "+", ("number","1")))),
("assign", "y", ("binop", ("identifier","x"), "+", ("binop", ("identifier","z"), "+", ("number","1"))))
]
answer3 = [ \
("assign", "x", ("binop", ("identifier","x"), "+", ("binop", ("identifier","z"), "+", ("number","1")))),
("assign", "y", ("binop", ("identifier","x"), "+", ("binop", ("identifier","z"), "+", ("number","1"))))
]
print optimize(example3)==answer3
pprint.pprint(optimize(example3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment