These are the solutions to the problems for the Fifth Homework Exam: 1. Implement the Magic Square procedure using nested lists as internal representation for matrices. My code: def show(matrix): for line in range(len(matrix)): for column in range(len(matrix[line])): print matrix[line][column], print def generate(columns, lines): matrix = [] for line in range(lines): row = [] for column in range(columns): row.append(0) matrix.append(row) return matrix def setZ(matrix): size = len(matrix) k = 1 i = size - 1 j = size / 2 while k <= size * size: matrix[i][j] = k a = (i + 1) % size b = (j + 1) % size if matrix[a][b] == 0: i = a j = b else: i = i - 1 k += 1 size = int(raw_input("Size: ")) board = generate(size, size) setZ(board) show(board) This is how this code runs: >>> ================================ RESTART ================================ >>> Size: 3 4 9 2 3 5 7 8 1 6 >>> ================================ RESTART ================================ >>> Size: 5 11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15 >>> ================================ RESTART ================================ >>> Size: 7 22 31 40 49 2 11 20 21 23 32 41 43 3 12 13 15 24 33 42 44 4 5 14 16 25 34 36 45 46 6 8 17 26 35 37 38 47 7 9 18 27 29 30 39 48 1 10 19 28 >>> ================================ RESTART ================================ >>> >>> Size: 9 37 48 59 70 81 2 13 24 35 36 38 49 60 71 73 3 14 25 26 28 39 50 61 72 74 4 15 16 27 29 40 51 62 64 75 5 6 17 19 30 41 52 63 65 76 77 7 18 20 31 42 53 55 66 67 78 8 10 21 32 43 54 56 57 68 79 9 11 22 33 44 46 47 58 69 80 1 12 23 34 45 >>> >>> ================================ RESTART ================================ >>> Size: 15 106 123 140 157 174 191 208 225 2 19 36 53 70 87 104 105 107 124 141 158 175 192 209 211 3 20 37 54 71 88 89 91 108 125 142 159 176 193 210 212 4 21 38 55 72 73 90 92 109 126 143 160 177 194 196 213 5 22 39 56 57 74 76 93 110 127 144 161 178 195 197 214 6 23 40 41 58 75 77 94 111 128 145 162 179 181 198 215 7 24 25 42 59 61 78 95 112 129 146 163 180 182 199 216 8 9 26 43 60 62 79 96 113 130 147 164 166 183 200 217 218 10 27 44 46 63 80 97 114 131 148 165 167 184 201 202 219 11 28 45 47 64 81 98 115 132 149 151 168 185 186 203 220 12 29 31 48 65 82 99 116 133 150 152 169 170 187 204 221 13 30 32 49 66 83 100 117 134 136 153 154 171 188 205 222 14 16 33 50 67 84 101 118 135 137 138 155 172 189 206 223 15 17 34 51 68 85 102 119 121 122 139 156 173 190 207 224 1 18 35 52 69 86 103 120 >>> Now you realize that the printing could be improved. So if we modify show like this: def show(matrix): for line in range(len(matrix)): for column in range(len(matrix[line])): print "%3d" % matrix[line][column], print Then the program runs as follows: >>> ================================ RESTART ================================ >>> Size: 3 4 9 2 3 5 7 8 1 6 >>> ================================ RESTART ================================ >>> Size: 5 11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15 >>> ================================ RESTART ================================ >>> Size: 9 37 48 59 70 81 2 13 24 35 36 38 49 60 71 73 3 14 25 26 28 39 50 61 72 74 4 15 16 27 29 40 51 62 64 75 5 6 17 19 30 41 52 63 65 76 77 7 18 20 31 42 53 55 66 67 78 8 10 21 32 43 54 56 57 68 79 9 11 22 33 44 46 47 58 69 80 1 12 23 34 45 >>> ================================ RESTART ================================ >>> Size: 15 106 123 140 157 174 191 208 225 2 19 36 53 70 87 104 105 107 124 141 158 175 192 209 211 3 20 37 54 71 88 89 91 108 125 142 159 176 193 210 212 4 21 38 55 72 73 90 92 109 126 143 160 177 194 196 213 5 22 39 56 57 74 76 93 110 127 144 161 178 195 197 214 6 23 40 41 58 75 77 94 111 128 145 162 179 181 198 215 7 24 25 42 59 61 78 95 112 129 146 163 180 182 199 216 8 9 26 43 60 62 79 96 113 130 147 164 166 183 200 217 218 10 27 44 46 63 80 97 114 131 148 165 167 184 201 202 219 11 28 45 47 64 81 98 115 132 149 151 168 185 186 203 220 12 29 31 48 65 82 99 116 133 150 152 169 170 187 204 221 13 30 32 49 66 83 100 117 134 136 153 154 171 188 205 222 14 16 33 50 67 84 101 118 135 137 138 155 172 189 206 223 15 17 34 51 68 85 102 119 121 122 139 156 173 190 207 224 1 18 35 52 69 86 103 120 >>> 2. Implement the Magic Square procedure using dictionaries as internal representation for matrices. Here's the code, can you count the changes? The program runs entirely the same. def show(matrix): # for line in range(matrix["height"]): ~~~ for column in range(matrix["width"]): ~~~ print "%3d" % matrix[line, column], ~~~ print # # def generate(columns, lines): # matrix = {} NEW matrix["height"] = lines NEW matrix["width"] = columns NEW for line in range(lines): # for column in range(columns): # matrix[line, column] = 0 ~~~ return matrix # # def setZ(matrix): # size = matrix["width"] ~~~ k = 1 # i = size - 1 # j = size / 2 # while k <= size * size: # matrix[i, j] = k ~~~ a = (i + 1) % size # b = (j + 1) % size # if matrix[a, b] == 0: ~~~ i = a # j = b # else: # i = i - 1 # k += 1 # # size = int(raw_input("Size: ")) # board = generate(size, size) # setZ(board) # show(board) # The lines that are identical in both solutions 1 and 2 are identified with a # on the right. Those lines that are slightly modified versions of the lines in solution 1 are marked with ~~~ on the right. The lines in this solution that are new and specific to this solution in particular have NEW on the right. Basically 25 out of 35 lines are COMPLETELY IDENTICAL. Furthermore 7 other lines (out of the total of 35) are NEARLY IDENTICAL. A mere 3 (out of 35) lines are completely new. But they're VERY SIMPLE. I think this is an aspect that can't be overlooked. 3. Design a class of objects called Robot with the following characteristics: a) a Robot knows where it's located (x, y) in a two-dimensional space with integer coordinates b) a Robot is initially created with a name ("Alice", "White Rabbit", "Mad Hatter") and a position c) a Robot can report its name and position when asked d) a Robot is always facing "North", "South", "East", or "West" e) a Robot can moveForward one step f) a Robot can turnLeft When you're done you should use your code with this example: a = Robot("Alice", 3, 1, "North") a.turnLeft() a.moveForward() b = Robot("Queen", -2, -1, "East") a.moveForward() a.turnLeft() b.moveForward() a.turnLeft() a.turnLeft() b.moveForward() a.moveForward() b.turnLeft() b.moveForward() The result should be compatible with this output: >>> ================================ RESTART ================================ >>> Creating... Robot Alice at( 3 , 1 ) facing North Robot Alice at( 3 , 1 ) facing North Alice turning left Robot Alice at( 3 , 1 ) facing West Robot Alice at( 3 , 1 ) facing West Alice moving forward Robot Alice at( 3 , 0 ) facing West Creating... Robot Queen at( -2 , -1 ) facing East Robot Alice at( 3 , 0 ) facing West Alice moving forward Robot Alice at( 3 , -1 ) facing West Robot Alice at( 3 , -1 ) facing West Alice turning left Robot Alice at( 3 , -1 ) facing South Robot Queen at( -2 , -1 ) facing East Queen moving forward Robot Queen at( -2 , 0 ) facing East Robot Alice at( 3 , -1 ) facing South Alice turning left Robot Alice at( 3 , -1 ) facing East Robot Alice at( 3 , -1 ) facing East Alice turning left Robot Alice at( 3 , -1 ) facing North Robot Queen at( -2 , 0 ) facing East Queen moving forward Robot Queen at( -2 , 1 ) facing East Robot Alice at( 3 , -1 ) facing North Alice moving forward Robot Alice at( 2 , -1 ) facing North Robot Queen at( -2 , 1 ) facing East Queen turning left Robot Queen at( -2 , 1 ) facing North Robot Queen at( -2 , 1 ) facing North Queen moving forward Robot Queen at( -3 , 1 ) facing North >>> Here's my code: class Robot(object): def __init__(self, name, x, y, direction): self.name = name self.x = x self.y = y self.direction = direction print "Creating...", self.report() def report(self): print "Robot", self.name, "at(", self.x, ",", self.y, ") facing", self.direction def moveForward(self): self.report(), print self.name, "moving forward" if self.direction == "North": self.x -= 1 elif self.direction == "West": self.y -= 1 elif self.direction == "South": self.x += 1 elif self.direction == "East": self.y += 1 self.report() def turnLeft(self): self.report(), print self.name, "turning left" if self.direction == "North": self.direction = "West" elif self.direction == "West": self.direction = "South" elif self.direction == "South": self.direction = "East" elif self.direction == "East": self.direction = "North" self.report() The minimal code is: class Robot(object): def __init__(self, name, x, y, direction): self.name = name self.x = x self.y = y self.direction = direction def report(self): print "Robot", self.name, "at(", self.x, ",", self.y, ") facing", self.direction def moveForward(self): if self.direction == "North": self.x -= 1 elif self.direction == "West": self.y -= 1 elif self.direction == "South": self.x += 1 elif self.direction == "East": self.y += 1 def turnLeft(self): if self.direction == "North": self.direction = "West" elif self.direction == "West": self.direction = "South" elif self.direction == "South": self.direction = "East" elif self.direction == "East": self.direction = "North" That's the same code from above without any extra-verbose print statements (that help with tracing).