>>> from operator import add, mul >>> mul(add(5, 6), 8) 88 >>> print(‘x’) x >>> y = print(‘x’) x >>> print(y) None >>> print(add(4, 2), print(‘a’)) a 6 None
Question 1: Raising the Bar What will Python output?
defcount_matches(n, m): """ >>> count_matches(10, 30) 1 >>> count_matches(12345, 23456) 0 >>> count_matches(212121, 321321) 2 >>> count_matches(101, 11) # only one’s place matches 1 >>> count_matches(101, 10) # no place matches 0 """ matches = 0 while (n > 0and m > 0): k, d = n % 10, m % 10 if k == d: matches += 1 n, m = n // 10, m // 10 return matches
Environment Diagrams
Question 4: A New Environment a) Draw the environment diagram for evaluating the following code
1 2 3 4
deff(x): return y + x y = 10 f(8)
b) Draw the environment diagram for evaluating the following code
1 2 3 4 5
defdessef(a, b): c = a + b b = b + 1 b = 6 dessef(b, 4)
Question 5: Environmental Collapse a) Draw an environment diagram for the following code
1 2 3 4 5 6 7
deffoo(x, y): foo = bar return foo(bar(x, x), y) defbar(z, x): return z + y y = 5 foo(1, 2)
b) Draw an environment diagram for the following code
c) Draw an environment diagram for the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cap = 9 hulk = 3 defmarvel(cap, thor, marvel): iron = hulk + cap if thor > cap: defmarvel(cap, thor, avengers): return iron else: iron = hulk return marvel(thor, cap, marvel) defiron(man): hulk = cap - 1 return hulk marvel(cap, iron(3), marvel)