LAB 2
#Lab #8 #Due Date: 08/01/2021, 11:59PM def matrixCalculator(matrix1, matrix2, operation): ''' >>> matrixCalculator([[2, 1], [-3, 6]], [[1, 3], [4, -1]],'sub') [[1, -2], [-7, 7]] >>> matrixCalculator([[3, 1], [2, 7]], [[4, 2], [5, 7]],'add') [[7, 3], [7, 14]] >>> matrixCalculator([[8, 2, -6, 2], [1, 5, 2, 24.5], [34, 4, 4, 2], [5, -98, 1.5, 4]], [[1, 7, 9, 55], [9.5, 45.5, 5, -9], [1, 5, 6, 67], [8, 4, 1, 7]],'add') [[9, 9, 3, 57], [10.5, 50.5, 7, 15.5], [35, 9, 10, 69], [13, -94, 2.5, 11]] >>> matrixCalculator([[8, 9, 10], [7.5, 2, -8], [1, 1, 1]], [[4, 2, 3], [43, 2, 32], [-2, 5.5, 3]],'sub') [[4, 7, 7], [-35.5, 0, -40], [3, -4.5, -2]] ''' # YOUR CODE STARTS HERE pass def mulDigits(num, fn): ''' >>> isTwo = lambda num: num == 2 >>> mulDigits(5724892472, isTwo) 8 >>> def divByFour(num): ... return not num%4 ... >>> mulDigits(5724892472, divByFour) 128 >>> mulDigits(155794, isTwo) 1 >>> mulDigits(67945125482222152, isTwo) 64 >>> mulDigits(679451254828822152, divByFour) 8192 ''' # YOUR CODE STARTS HERE pass def getCount(x): ''' >>> getCount(6)(62156) 2 >>> digit = getCount(7) >>> digit(9457845778457077076) 7 >>> digit(-945784578457077076) 6 >>> getCount(6)(-65062156) 3 ''' # YOUR CODE STARTS HERE pass def genHailstone(num): ''' >>> seq = genHailstone(5) >>> [item for item in seq] [5, 16, 8, 4, 2, 1] >>> seq = genHailstone(6) >>> next(seq) 6 >>> next(seq) 3 >>> next(seq) 10 >>> next(seq) 5 >>> itr1 = genHailstone(3) >>> next(itr1) 3 >>> next(itr1) 10 >>> next(itr1) 5 >>> next(itr1) 16 >>> next(itr1) 8 >>> next(itr1) 4 >>> next(itr1) 2 >>> next(itr1) 1 >>> next(itr1) Traceback (most recent call last): ... StopIteration >>> next(seq) 16 ''' # YOUR CODE STARTS HERE pass def genFib(fn): ''' >>> evens = genFib(lambda x: x % 2 == 0) >>> [next(evens) for _ in range(15)] [0, 2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, 14930352, 63245986, 267914296] >>> seq = genFib(lambda x: x > 20 and x % 2) >>> next(seq) 21 >>> next(seq) 55 >>> next(seq) 89 >>> next(seq) 233 >>> next(seq) 377 >>> next(seq) 987 >>> next(seq) 1597 >>> next(seq) 4181 ''' # YOUR CODE STARTS HERE pass CMPSC 132: Programming and Computation II Lab 8 (10 points) Due date: August 1st, 2021, 11:59 PM EST Goal: The goal of this lab is for you to practice higher-level functions and generators through the various problems present in this lab. General instructions: • The work in this assignment must be your own original work and be completed alone. • The instructor and course assistants are available on Teams and with office hours to answer any questions you may have. You may also share testing code on Teams. • A doctest is provided to ensure basic functionality and may not be representative of the full range of test cases we will be checking. Further testing is your responsibility. • Debugging code is also your responsibility. • You may submit more than once before the deadline; only the latest submission will be graded. Assignment-specific instructions: • Download the starter code file from Canvas. Do not change the function names or given starter code in your script. • Additional examples of functionality are provided in each function’s doctest • If you are unable to complete a function, use the pass statement to avoid syntax errors • See section 2 for extra practice problems on the topics discussed in Module 9 (optional, not for credit) Submission format: • Submit your code in a file named LAB8.py file to the Lab 8 Gradescope assignment before the due date. • As a reminder, code submitted with syntax errors does not receive credit, please run your file before submitting. Section 1: Required functions (10 points) matrixCalculator(matrix1, matrix2, operation) (2 points) Sums or subtract two square matrices of the same size. You must use list comprehension only for this function (you should only have three to five lines od code), otherwise, you will not receive credit. You can assume matrices and operations are always valid. Matrix addition: Matrix subtraction: Input list matrix1 First matrix, represented as a nested list list matrix2 Second matrix, represented as a nested list str operation ‘add’ for addition, ‘sub’ for subtraction Output list The sum/subtraction of the two matrices Section 1: Required functions (10 points) mulDigits(num, fn) (2 points) Returns the multiplication of all digits in num for which fn returns True when receiving the digits as argument. You can assume that fn will always be a function that takes one number as argument and returns a boolean value. You are not allowed to use lists, tuples, strings or convert num to string using str(num). Input int num A positive integer function fn A function’s code reference Output int Accumulated multiplication of all digits in num that evaluated to True when passed to fn Examples: >>> isTwo = lambda num: num == 2 # Simple anonymous function >>> mulDigits(5724892472, isTwo) # Only 2 evaluates to True 8 >>> def divByFour(num): # Conventional function definition ... return not num%4 >>> mulDigits(5724892472, divByFour) # Only 4 and 8 evaluate to True 128 getCount(x) (2 points) Takes in a positive integer and returns a function that takes in an integer num, returning how many times x appears as a digit in num. You are not allowed to use lists, tuples, strings or convert num to string using str(num). Note that num//10 does not behave the same when num is negative, 562//10 returns 56 while -562//10 returns -57. Input int x A positive integer that is less than 10 Output function A function that checks how many times x appears in num. You can only assume num is an integer Section 1: Required functions (10 points)