Tasks
(20 points) Complete the homework as indicated in the source code.
Download the homework page
Download the source file hw03.py and save it on your computer. It contains the stubs of five Python functions. Save it directly to your folder with your other lab files and open it in the Python environment. Complete the stub methods to perform the indicated function. Verify your code by running the program. Add additional test cases as needed.
In most cases, your function will not print any values. Rather, your function will return values to the calling function and any values will be used there, including possibly printing.
Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!
Code
# hw05.py
# Use only range(), len() functions from python
# use the in operator as needed
# create and return the product of all elements in the list
# Examples:
# [] -> 1
# [1,2,3] -> 6
# [0,1,2] -> 0
# [2,4,6,8] -> 384
def mul(A):
return None
# return a new list that contains the unique items in the given list
# [] -> []
# [1,2,3] -> [1,2,3]
# [1,1,1,1,1,1] -> [1]
# [1,2,2,3,3,3] -> [1,2,3]
# [1,1,1,2,2,3] -> [1,2,3]
# Hint: use the in operator
def unique(A):
return None
# count the number of values in A between b and c inclusive
# ([],b,c) -> 0
# [6, 10, 4, 9, 8, 3, 4, 2, 7, 4] -> 10
# ([24, 18, 5, 25, 17, 11, 21, 12, 21, 22], 5, 20) -> 5
# ([22, 23, 21, 13, 17, 7, 10, 2, 3, 14], 5, 20) -> 5
# ([10, 1, 9, 11, 19, 8, 11, 18, 16, 10], 5, 20) -> 9
# ([4, 7, 10, 25, 12, 13, 1, 23, 3, 21], 5, 20) -> 4
# ([3, 22, 8, 19, 1, 12, 8, 20, 10, 7], 5, 20) -> 7
# ([1, 1, 2, 5, 1, 2, 4, 2, 2, 4], 5, 20) -> 1
# ([6, 7, 4, 5, 8, 8, 5, 10, 1, 7], 5, 20) -> 8
# ([8, 17, 20, 20, 16, 7, 5, 17, 19, 21], 5, 20) -> 9
# ([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 1, 5) -> 0
# ([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 1, 10) -> 1
# ([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21, 25) -> 0
def countValues(A, b, c):
return None
# returns True if the sequence is strictly increasing, False otherwise
# it is False if the sequence has 0 or 1 item
# [] -> False
# [1] -> False
# [1,2] -> True
# [1,2,2] -> False
# [3,2,1] -> False
# [1,2,3,4,5] -> True
def isIncreasing(A):
return None
# return a new list that has unique elements in common with both lists
# this is similar to set intersection
# [3, 6, 7, 7, 10] [3, 9, 2, 3, 1] -> [3]
# [5, 5, 9, 7, 6] [6, 5, 1, 8, 6] -> [5, 6]
# [] [] -> []
# [1, 2, 3] [1, 2, 3] -> [1, 2, 3]
# [1, 2, 3] [4, 5, 6, 7, 8] -> []
# [1, 2, 3, 4, 5] [4, 5, 6, 7, 8, 9, 10] -> [4, 5]
# Hint: use you unique function
def common(A, B):
return None
def main():
print("mul")
print(mul([])) # 1
print(mul([1,2,3])) # 6
print(mul([0,1,2])) # 6
print(mul([2,4,6,8])) # 384
print()
print("unique")
print(unique([])) # []
print(unique([1,2,3])) # [1,2,3]
print(unique([1,1,1,1,1,1])) # [1]
print(unique([1,2,2,3,3,3])) # [1,2,3]
print(unique([1,1,1,2,2,3])) # [1,2,3]
print()
print("count values")
print(countValues([], 1, 2)) # 0
print(countValues([6, 10, 4, 9, 8, 3, 4, 2, 7, 4], 1, 10)) # 10
print(countValues([24, 18, 5, 25, 17, 11, 21, 12, 21, 22], 5, 20)) # 5
print(countValues([22, 23, 21, 13, 17, 7, 10, 2, 3, 14], 5, 20)) # 5
print(countValues([10, 1, 9, 11, 19, 8, 11, 18, 16, 10], 5, 20)) # 9
print(countValues([4, 7, 10, 25, 12, 13, 1, 23, 3, 21], 5, 20)) # 4
print(countValues([3, 22, 8, 19, 1, 12, 8, 20, 10, 7], 5, 20)) # 7
print(countValues([1, 1, 2, 5, 1, 2, 4, 2, 2, 4], 5, 20)) # 1
print(countValues([6, 7, 4, 5, 8, 8, 5, 10, 1, 7], 5, 20)) # 8
print(countValues([8, 17, 20, 20, 16, 7, 5, 17, 19, 21], 5, 20)) # 9
print(countValues([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 1, 5)) # 0
print(countValues([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 1, 10)) # 1
print(countValues([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21, 25)) # 0
print()
print("isIncreasing")
L = []
print(L, isIncreasing(L)) # False
L = [1]
print(L, isIncreasing(L)) # False
L = [1,2]
print(L, isIncreasing(L)) # True
L = [2,1]
print(L, isIncreasing(L)) # False
L = [1,2,3]
print(L, isIncreasing(L)) # True
L = [i for i in range(5)]
print(L, isIncreasing(L)) # True
L = [i*i for i in range(5)]
print(L, isIncreasing(L)) # True
L = [1]*5
print(L, isIncreasing(L)) # False
L = [i for i in range(5,0,-1)]
print(L, isIncreasing(L)) # False
L = [i//2 for i in range(10)]
print(L, isIncreasing(L)) # False
L = [i for i in range(5)] + [i for i in range(5,0,-1)]
print(L, isIncreasing(L)) # False
print()
print("common")
L1 = [3, 6, 7, 7, 10]
L2 = [3, 9, 2, 3, 1]
print(L1, L2, common(L1, L2)) # [3]
L1 = [5, 5, 9, 7, 6]
L2 = [6, 5, 1, 8, 6]
print(L1, L2, common(L1, L2)) # [5, 6]
L1 = [10, 7, 4, 9, 5]
L2 = [2, 4, 3, 4, 8]
print(L1, L2, common(L1, L2)) # [4]
L1 = [4, 3, 10, 8, 2]
L2 = [9, 9, 7, 2, 7]
print(L1, L2, common(L1, L2)) # [2]
L1 = [3, 2, 1, 10, 6]
L2 = [10, 6, 10, 6, 9]
print(L1, L2, common(L1, L2)) # [10, 6]
L1 = []
L2 = []
print(L1, L2, common(L1, L2)) # []
L1 = [1,2,3]
L2 = [1,2,3]
print(L1, L2, common(L1, L2)) # [1,2,3]
L1 = [1,2,3]
L2 = [4,5,6,7,8]
print(L1, L2, common(L1, L2)) # []
L1 = [1,2,3,4,5]
L2 = [4,5,6,7,8,9,10]
print(L1, L2, common(L1, L2)) # [4,5]
main()
Deliverables
Send me ([email protected]) your Python .py
file as an attachment to an
email message with Homework 3 as the subject line.