#! /usr/bin/env python # -*- coding: utf-8 -*- """ Python solution for the Problem II of the CS101 Final Exam (fill-in-the-blanks). @date: Thu Apr 23 19:08:05 2015. @author: Lilian Besson for CS101 course at Mahindra Ecole Centrale 2015. @licence: MIT Licence (http://lbesson.mit-license.org). """ # %% Question 7) class A(object): def f(self): return self.g() def g(self): return "Hi from class A." class B(A): def g(self): return "Hi from class B." a = A() b = B() print a.f(), b.f() print a.g(), b.g() # %% Question 8) x = 0 y = 1 for n in [5, 4, 6]: print x, y, n, y*n x = x + y*n y = y + 2 print "x =", x, "and y =", y # %% Question 9) d_num = { '1': 1, '2': 2 } theCopy = d_num # The trick is that this dictionnary is NOT a copy of d_num, but the same dict! sum1 = d_num['1'] + theCopy['1'] d_num['1'] = 5 sum2 = d_num['1'] + theCopy['1'] print "sum1 =", sum1, "and sum2 =", sum2 # %% Question 10) d_num = {} d_num[(1, 2, 4)] = 8 d_num[(4, 2, 1)] = 10 d_num[(1, 2)] = 12 sum1 = 0 for k in d_num: sum1 += d_num[k] print "len(d_num) =", len(d_num), "and sum1 =", sum1 # %% End!