2 Python assignments
CSE201 HOMEWORK 2
Due Date : 6/14/2019
Write definitions for these functions (30 points)
1. To get full credit (30 points), choose ONE of the following:
Option 1: Any 2 problems of Level 1, any 2 problems of level 2, and 1 problem of level 3 (5 in total)
Option 2: Any 2 problem of Level 4
Option 3: Any one problem of Level 5
2. When you choose one option, you cannot get points from the rest options.
3. No credit will be given for individual question if :
(1). Hardcoding temporary variables as input parameters.
(2). Printing result instead of returning.
(3). Indentation error occurs.
4. Submit your source code ON BLACKBOARD.
Your source code file should be like the following :
And use comments to increase code readability.
# name : # sid : # option : 1 or 2 or 3 # 1 - 1 : double a number def double(a): return a*2 # 3 - 1 : prettify a string def prettify(s): #your code here
1 2 3 4 5 6 7 8 9
10
Level 1 :
1. (3p) Write the function double(a) that accepts an integer as formal parameter and returns the double of it. For example, your function called on 7 returns 14.
2. (3p) is_negative(a) Returns the True if the given number a is negative and vice versa. For example, is_negative(-3) should return True and is_negative(11) should return False.
3. (3p) GeometricMean(a,b) Returns the geometric mean having the given two positive numbers a and b. For example, GeometricMean(2,8) should return square root of (2 * 8 ) = 4.
4. (3p) firstAndLast(L) accepts a list of numbers L and returns the sum of first element and the last element of it. For example, firstAndLast([1,3,4,8,12]) returns 1 + 12 = 13.
Level 2 :
1. (7p) sumDiff(a,b) Returns the sum AND the difference of the two given numbers a and b. For example, sumDiff(6,1.5) should return 7.5 and 4.5
2. (7p) sumEle(L) Returns the sum of all elements in the given list L. For example, sumEle([3,3,4]) should return 10, while sumEle([1,-3,4]) should return 2, etc.
3. (7p) LargerThan5(L) Returns a new list which contains every element in the given list L that is larger than 5. For example, LargerThan5([-3,-1,3,6,7,11,9,1]) should return [6,7,11,9].
Level 3 :
1. (10p) prettify(s) accepts a string and removes its leading and trailing spaces and converts all uppercase characters in the string into lowercase characters and returns it. For example, prettify(' CatOntheMat ') returns 'catonthemat'. Hint: use .strip(' ') and .lower(), see slides.
2. (10p) backwards(s) accepts a string and returns a list containing all characters in that string, but the order is reversed. For example, backwards('abcd') returns ['d','c','b','a']
Level 4 :
1. (15p) ZeroPadding(L) accepts a list and replaces all the elements in that list by integer 0. For example, let a = ['a','b',-2233, 99, 0], then ZeroPadding(a) will modify a to [0,0,0,0,0]. Note that the function does not require a return (think why).
2. (15p) sumCnt(L) accepts a list of integers and returns the sum of all ODD numbers in the list as well as the number of how many ODD numbers the list contained.
3. (15p) Diff(L1,L2) accepts two lists of numbers (say l1 and l2) and returns a list that is the element-wise absolute value of difference of the two lists. For example, Diff([1,3,4],[3,-1,2) should return [2,4,2],
because abs(1-3) = 2, abs(3-(-1)) = 4, and abs(4-2) = 2.
Level 5 :
1. (30p) Suppose we have a string that contains only one unique character with the rest charcters repeated exatly twice, like 'papayll', 'aabbcdd', 'ccaabbymmolopxkklxp', etc. Write a function findUniq(s) to return that unique character.
2. (30p) nonPrime_lst(L) accepts a list L of integers ( >1 ) and returns a list that contains all the Non- prime numbers picked from L. For example, nonPrime_lst([2,4,5,7,10,11,12,15,19] should return [4,10,12,15]).