############################################################################### # YOUR NAME: # SECTION: CSE 143 AX # DATE: November 16, 2010 # TITLE: CSE 143 Week 6 Exploration Session Assignment (Python) ############################################################################### ############################################################################### # Problems you should complete: # (Some examples using map/reduce/filter are included below.) ############################################################################### def product(list): """ Returns the product of the elements in the given list (you may assume that the list is not empty). """ return -1000 def squares(list): """ Replaces every element in the given list by its square. """ return [-1000] def sum_of_squares(list): """ Squares all of the elements in the list and returns their sum. (try doing this without calling the squares() function!) """ return -1000 def num_evens(list): """ Returns the number of even integers in the given list. """ return -1000 def retain_multiples(list): """ Returns a list identical to the given list, except that the new list will contain only elements which are multiples of another element in the list. For this problem, a number is not a multiple of itself (so, 4 is the smallest multiple of 2). For example, if the given list is [3, 4, 2, 5, 8], then the result would be the list [4, 8] (4 is a multiple of 2, and 8 is a multiple of both 2 and 4). The 3, 2, and 5 are removed because they aren't multiples of any element in the list. You may assume that all integers in the list are positive. Hint: You can use an anonymous function inside of another anonymous function!!! """ return [-1000] ############################################################################### # Testing code: (Feel free to add your own test cases.) ############################################################################### def assert_equals(function, input, expected): result = function(input) print function.__name__ + '(' + str(input) + ')', if result == expected: print '=', result, "\n\t(This is the expected result!)" else: print "!=", result, "\n\t(The expected result is:", str(expected) + ')' print def header(function): print for i in range(0, 40): print "-", print print "Test results for", function.__name__ + "():" for i in range(0, 40): print "-", print header(product) assert_equals(product, [5], 5) assert_equals(product, [1, 2, 3], 6) assert_equals(product, [-378, -444, 143, 341], 8183991816) assert_equals(product, [-10, 0, 3], 0) header(squares) assert_equals(squares, [1, 2, 3, 4], [1, 4, 9, 16]) assert_equals(squares, [-1, -2, -3, -4], [1, 4, 9, 16]) assert_equals(squares, [], []) assert_equals(squares, [0], [0]) assert_equals(squares, [-89, 0, 2, 15, -10, 20], [7921, 0, 4, 225, 100, 400]) header(sum_of_squares) assert_equals(sum_of_squares, [1, 2, 3, 4], 30) assert_equals(sum_of_squares, [-1, -2, -3, -4], 30) assert_equals(sum_of_squares, [0], 0) assert_equals(sum_of_squares, [-89, 0, 2, 15, -10, 20], 8650) header(num_evens) assert_equals(num_evens, [1, 2, 3, 4], 2) assert_equals(num_evens, [-1, -2, -3, -4], 2) assert_equals(num_evens, [0], 1) assert_equals(num_evens, [9], 0) assert_equals(num_evens, [], 0) assert_equals(num_evens, [34, 12, 45, 42, 100, 0, -10, 9, 11, 89, 444, 378, 303, 322, 143], 9) header(retain_multiples) assert_equals(retain_multiples, [], []) assert_equals(retain_multiples, [2, 4], [4]) assert_equals(retain_multiples, [2, 4, 8], [4, 8]) assert_equals(retain_multiples, [34, 12, 45, 42, 100, 9, 11, 89, 444, 378, 303, 322, 143, 888], [45, 444, 378, 143, 888]) ############################################################################### # Examples using map/reduce/filter: ############################################################################### def sum(list): """ Returns the sum of the elements in the given list (you may assume that the list is not empty). """ return reduce(lambda x, y: x + y, list) def num_divisible_by_n(list, n): """ Returns the number of elements in the list that are divisible by n. """ return len(filter(lambda x: x % n == 0, list)) def is_palindrome(string): """ Returns true if the given string is a palindrome. """ return string == reduce(lambda x, y: y + x, string) def all_positive(list): """ Returns true if all of the numbers in the list are positive (strictly greater than 0). """ return reduce(lambda x, y: x and y, map(lambda x: x > 0, list)) # OR: # return filter(lambda x: x <= 0, list) == len(list) def l33tify(string): """ "Leetifies" the given string by replacing characters by the following rules: 'o' => '0' 'e' => '3' 'l' => '1' 'a' => '4' """ string = string.lower() ''' This is a Python dictionary, similar to a hash map in Java. You can create a dictionary giving it key/value pairs as shown here. Instead of using Java's .get and .put methods, you can get a value using dictionary[key] notation, associate a value with a key by using dictionary[key] = value, or get the keyset with dictionary.keys() method. ''' rules = {'o': '0', 'e': '3', 'l': '1', 'a': '4'} return reduce(lambda x, y: x + y, map(lambda x: rules[x] if x in rules else x, string)) # Test cases for the session examples possible_palindroms = ["racecar", "civic", "mapreduce", "stuartreges"] string1 = "Four score and seven years ago our fathers brought forth on this continent a new nation, \ conceived in liberty, and dedicated to the proposition that all men are created equal." data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] data2 = [34, 12, 45, 42, 100, 0, -10, 9, 11, 89, 444, 378, 303, 322, 143]