These are the programs we will develop/review in lecture and lab today for the exam tomorrow. 1. Write a function to sum up all the elements in a list of numbers. (Note: such a function already exists, it's called sum). IDLE 1.2.2 >>> sum([1, 2, 3]) 6 >>> ================================ RESTART ================================ >>> >>> sumup([1, 2, 3]) 6 >>> a = [4, 2, -3] >>> a [4, 2, -3] >>> sumup(a) 3 >>> Here's my code: def sumup(numbers): result = 0 for number in numbers: result = result + number return result 2. Write a function that returns a list of a given size that has random numbers in it. >>> ================================ RESTART ================================ >>> >>> generate(5) [22, 87, 29, 46, 1] >>> a = generate(7) >>> a [12, 41, 54, 84, 39, 36, 87] >>> Here's my code: def generate(size): result = [] import random for i in range(size): num = random.randrange(100) result.append(num) return result 3. Write a function that receives a string, a position in the string, and a character and returns the string with the new character at the given position. We've done this already in the Hangman game. >>> change("burp", 2, "m") 'bump' >>> a = "burp" >>> change(a, 2, 'm') 'bump' >>> a 'burp' >>> a = change(a, 2, 'm') >>> a 'bump' >>> Here's my code: def change(string, index, char): return string[0:index] + char + string[index+1:] Notice that this is the shortest I can write this function. Longer versions are also possible. Sometimes the functions we write end up working on more than just the cases we had in mind when we designed it: >>> change("burp", 2, 'rla') 'burlap' >>> But that's OK, as long as we make no promises in those cases or check them and confirm the desired behavior, if any. 4. Write a function that sorts a list of integers in ascending order. >>> ================================ RESTART ================================ >>> >>> a = [4, 2, 3, 1, 5, 4, 6, 2] >>> a.sort() >>> a [1, 2, 2, 3, 4, 4, 5, 6] >>> [3, 2, 4, 1, 5, 4, 6, 3] [3, 2, 4, 1, 5, 4, 6, 3] >>> [3, 2, 4, 1, 5, 4, 6, 3].sort() >>> sort >>> sort([3, 5, 4, 2, 4]) >>> a = [3, 5, 4, 2, 4] >>> a [3, 5, 4, 2, 4] >>> sort(a) >>> a [2, 3, 4, 4, 5] >>> Here's my code: def sort(numbers): sorted = False while not sorted: sorted = True for i in range(len(numbers) - 1): if numbers[i] > numbers[i+1]: temp = numbers[i] numbers[i] = numbers[i+1] numbers[i+1] = temp sorted = False 5. Write a function that returns the list of keys in a dictionary sorted in descending order by their values in the dictionary >>> d = {'Four': 67, 'Five': 64, 'Three': 51, 'Two': 48, 'One': 70} >>> d {'Four': 67, 'One': 70, 'Five': 64, 'Three': 51, 'Two': 48} >>> sort(d) ['One', 'Four', 'Five', 'Three', 'Two'] >>> Here's my code: def sort(dict): names = dict.keys() sorted = False while not sorted: sorted = True for i in range(len(names) - 1): if d[names[i]] < d[names[i+1]]: (names[i], names[i+1], sorted) = (names[i+1], names[i], False) return names 6. Write a function that generates a magic square of size n if n is odd, or returns an error message otherwise. >>> magic(4) 'Sorry, this works only for odd sizes.' >>> magic(3) 0 0 0 0 0 0 0 1 0 -------------------- 0 0 2 0 0 0 0 1 0 -------------------- 0 0 2 3 0 0 0 1 0 -------------------- 4 0 2 3 0 0 0 1 0 -------------------- 4 0 2 3 5 0 0 1 0 -------------------- 4 0 2 3 5 0 0 1 6 -------------------- 4 0 2 3 5 7 0 1 6 -------------------- 4 0 2 3 5 7 8 1 6 -------------------- 4 9 2 3 5 7 8 1 6 -------------------- [[4, 9, 2], [3, 5, 7], [8, 1, 6]] >>> m = magic(3) 0 0 0 0 0 0 0 1 0 -------------------- 0 0 2 0 0 0 0 1 0 -------------------- 0 0 2 3 0 0 0 1 0 -------------------- 4 0 2 3 0 0 0 1 0 -------------------- 4 0 2 3 5 0 0 1 0 -------------------- 4 0 2 3 5 0 0 1 6 -------------------- 4 0 2 3 5 7 0 1 6 -------------------- 4 0 2 3 5 7 8 1 6 -------------------- 4 9 2 3 5 7 8 1 6 -------------------- >>> show(m) 4 9 2 3 5 7 8 1 6 -------------------- >>> Here's how my code works: def magic(size): if size % 2 == 0: return "Sorry, this works only for odd sizes." else: m = generate(size, size) k = 1 i = size-1 j = size/2 while k <= size * size: m[i][j] = k a = (i + 1) % size b = (j + 1) % size if m[a][b] == 0: i = a j = b else: i = i - 1 if i < 0: i = size - 1 k = k + 1 show(m) return m def show(m): for i in range(len(m)): for j in range(len(m[i])): print "%3d" % m[i][j], print print "-" * 20 def generate(lines, columns): m = [] for i in range(lines): row = [] for j in range(columns): row.append(0) m.append(row) return m Try it for other sizes: 7, 9, 13 and so on.