Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> #converting a number in a decimal representation to a binary rep. (b=2) #the function returns a string >>> >>> bin(10) '0b1010' #converting a string representing a number in a given base (default b= 10) to an integer in a decimal rep. (b=10) >>> int(bin(10)) Traceback (most recent call last): File "", line 1, in int(bin(10)) ValueError: invalid literal for int() with base 10: '0b1010' >>> int(bin(10),2) 10 >>> int("1010",2) 10 >>> int("40") 40 >>> int("10") 10 >>> int("10", 2) 2 #converting a number in a decimal representation to a hexadecimal rep. (b=16) #the function returns a string >>> hex(16) '0x10' >>> hex(15) '0xf' >>> int("f", 16) 15 >>> int("f", 2) Traceback (most recent call last): File "", line 1, in int("f", 2) ValueError: invalid literal for int() with base 2: 'f' #Running the initial version of convert_base >>> ================================ RESTART ================================ >>> >>> convert_base(10,2) '1010' >>> convert_base(0,2) '' >>> convert_base(10,3) '101' >>> convert_base(10,16) '10' >>> convert_base(4,16) '4' >>> convert_base(10,16) '10' #Running the updated version of convert_base >>> ================================ RESTART ================================ >>> >>> convert_base(10,16) 'a' >>> convert_base(17,16) '11' >>> convert_base(17,100) Traceback (most recent call last): File "", line 1, in convert_base(17,100) File "C:\Personal\Teaching\CS-intro\2016a\Recitations\3\convert_base.py", line 8, in convert_base assert 2 <= b <= 36 AssertionError #memory model #examples: >>> def change_int(num): num = 999 >>> n =1 >>> change_int(n) >>> n 1 >>> def add_to_list(lst): lst.append(999) >>> ls = [1,2,3] >>> add_to_list(ls) >>> ls [1, 2, 3, 999] >>> def change_lst(lst): lst = [999,999,999] >>> ls [1, 2, 3, 999] >>> change_lst(ls) >>> ls [1, 2, 3, 999] >>> type(len) #using the id() function in order to test whether the variable points # to a new place in memory >>> s = "michal" >>> s1 = "michal" >>> id(s) 57414240 >>> id(s1) 57414240 >>> lst = [3,2,1] >>> id(lst) 57386184 >>> lst = lst + [0] >>> id(lst) 55968264 >>> lst.append(90) >>> lst [3, 2, 1, 0, 90] >>> id(lst) 55968264 >>> lst.insert(1,5) >>> lst [3, 5, 2, 1, 0, 90] >>> id(lst) 55968264 >>> lst.extend([6,7]) >>> lst [3, 5, 2, 1, 0, 90, 6, 7] >>> id(lst) 55968264 >>> lst += [800] >>> lst [3, 5, 2, 1, 0, 90, 6, 7, 800] >>> id(lst) 55968264 >>> >>> lst = [1, 2, 3, 0, 100, 8] >>> id(lst) 56236424 >>> lst2 = sorted(lst) >>> id(lst2) 39654920 >>> lst [1, 2, 3, 0, 100, 8] >>> lst.sort() >>> id(lst) 56236424 >>> lst [0, 1, 2, 3, 8, 100] >>> #running time_extend_lists.py #first for n = 10000 >>> 0.24765770061513975 0.0026059758238704545 #then for n=20000 (doubled the value of n) #Note that the first solution is 4 times slower than for n=10000 (since it has O(n**2) complexity) # and thath the second solution is 2 times slower than for n=10000 (since it has O(n) complexity) >>> ================================ RESTART ================================ >>> 0.9296449683588274 0.00531931690434384 >>>