Let's start with this: generate our patterns by visiting the cells in the matrix. # this method generates a matrix of a given size implemented with nested lists def generate(size): m = [] for i in range(size): row = [] for j in range(size): row.append(" ") # the matrix is full of blanks m.append(row) return m # this method shows a matrix represented as a list of lists (rows) def show(m): for i in range(len(m)): for j in range(len(m[i])): print m[i][j], print # this is the main program def main(): # get the size size = int(raw_input("Size: ")) # generate the matrix m = generate(size) # announce your achievement print "I have just generated a matrix full of spaces:\n" # show it show(m) # draw the north border (a, b) = (0, 0) # put the pen in this position for i in range(size): # and for size times m[a][b] = "*" # place some ink in the pen location (a, b) = (a, b+1) # then move the pen to the right # announce your achievement print "I have just drawn the north border:\n" show(m) # show the matrix: it's full of blanks except the north border # draw the south border (a, b) = (size-1, 0) for i in range(size): m[a][b] = "*" (a, b) = (a, b+1) # announce your achievement print "I have just drawn the south border:\n" show(m) # now draw the diagonal (a, b) = (0, size-1) for i in range(size): m[a][b] = "*" (a, b) = (a+1, b-1) # announce your achievement print "I have just drawn the second diagonal (NE to SW):\n" show(m) # finally draw the little dash in the middle (a, b) = (size/2, size/4) for i in range(size - size/2+1): m[a][b] = "*" (a, b) = (a, b+1) # announce your achievement print "I have just drawn the little dash in the middle:\n" show(m) print "I am done. Enjoy your scalable pattern!" # start the program main() This is how the program shown above actually works: >>> ================================ RESTART ================================ >>> Size: 23 I have just generated a matrix full of spaces: I have just drawn the north border: * * * * * * * * * * * * * * * * * * * * * * * I have just drawn the south border: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * I have just drawn the second diagonal (NE to SW): * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * I have just drawn the little dash in the middle: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * I am done. Enjoy your scalable pattern! >>> You see the pattern is drawn as if with a pen. This is just another way of getting a scalable Z, but in the style of Homework Five. Notice that we almost have an object: the pen, whose position is (a, b) Now more exercises of the kind we didn't have yesterday: more about dictionaries and lists What does this print: a = [[2, 4, 3], \ [1, 3, 2], \ [1, 2, 3], \ [3, 4, 1]] print a[a[3][2]][1] Consider the code below: def fun(size, value): result = [] for i in range(size): result.append(value - i) return result # What will fun(3, 3)[2] evaluate to? (Yes, it compiles and runs fine). print fun(3, 3)[2] What does this print: lst = [1, 2, 3, 4] print lst for elem in lst: elem = elem + 1 print lst Explain. What does this print? lst = [1, 2, 3, 4] print lst for i in range(lst): lst[i] = lst[i] + 1 print lst Explain. What does this print? lst = [1, 2, 3, 4] print lst for i in range(len(lst)): lst[i] = lst[i] + 1 print lst Explain. def fun(lst): lst[-1] = lst[-1] + 3 a = [4, 5, 6] print a fun(a) print a What does this print: def fun(lst): a[:] = [1, 2, 3] a = [4, 5, 6] print a fun(a) print a Explain. What does this print: def fun(m): m = 10 a = 5 print a fun(a) print a Explain. What does this print: def fun(m): m = 10 m = 5 print m fun(m) print m Explain. What does this print: def fun(): global m m = 10 m = 5 print m fun() print m Explain. What does this print: def fun(a, b): return a - b def nuf(b, a): return a - b print nuf(fun(5, nuf(fun(4, 3), 2)), 1) Explain. What does this print? def mix(a, b): if len(a) == len(b): result = "" for i in range(len(a)): if i % 2 == 0: result = a[i] + result + b[i] else: result = b[i] + result + a[i] return result else: return "Strings not the same length!" print mix("oihro!", "soe sg") Explain. This concludes the lecture notes for today. --