Sie sind auf Seite 1von 3

CMPSC-132: Programming and Computation II

Fall 2019

Lab #13
Due Date: 12/06/2019, 11:59PM

Read the instructions carefully before starting the assignment. Make sure your code follows the
stated guidelines to ensure full credit for your work.

Instructions:
- The work in this lab must be completed alone and must be your own.
- Download the starter code file from the LAB13 Assignment on Canvas. Do not change
the function names on your script.
- A doctest is provided as an example of code functionality. Getting the same result as the
doctest does not guarantee full credit. You are responsible for debugging and testing your
code with enough data, you can share ideas and testing code during your recitation class.
As a reminder, Gradescope should not be used to debug and test code!
- Each function must return the output (Do not use print in your final submission, otherwise
your submissions will receive a -1 point deduction)
- Do not include test code outside any function in the upload. Printing unwanted or ill-
formatted data to output will cause the test cases to fail. Remove all your testing code
before uploading your file (You can also remove the doctest). Do not include the
input() function in your submission.

Goal
[5 pts] Write the function digitSum(num) that takes appositive integer num and returns another
function that takes in a positive integer x which will return True if the digits of x sum to num, False
otherwise

un term on each item in aList and returns a list containing [term(item 1), term(item 2), ... ,
term(item n)]. Hint: Use map()

[5 pts] Write the function alternate(fn1, fn2) that takes in two functions, fn1 and fn2, and returns
a function that takes one parameter n and returns a list that contains all the numbers between 1 and
n (inclusive), applying the function fn1 to every odd number and fn2 to every even number before
adding them to the list.

Examples available in the doctest

Deliverables:
• Submit your script file named LAB13.py to the Lab13 GradeScope assignment before the
due date
Optional Questions:
These questions are for practice only, you don’t have to submit your answer.

Without using the Python interpreter, based on the code below, complete the mapper call so it
prints the output displayed in red:
def mapper(fn, num):
i = 0
while i < num:
print(fn(i))
i = i + 1

mapper(lambda x: ______, 4)
1
3
5
7

Using list comprehension syntax, write the code to create a list that multiplies every part of any
list by three.
list1 = [3,4,5,6]
list2 = ?
print(list2)
[9, 12, 15, 18]

Using list comprehension syntax, write the code to create a list that contains the first letter of each
word
words = ["this","is","a","list","of","words"]
letters = ?
print(letters)
['t', 'i', 'a', 'l', 'o', 'w']

Using list comprehension syntax, write the code to create a list that contains the numbers in a string
text = "Hello 12345 World"
numbers = ?
print(numbers)
['1', '2', '3', '4', '5']

Consider the following example:


feet=[5, 46, 57.8, 4.6, 2564.846]
feet = list(map(int, feet))
uneven = filter(lambda x: x%2, feet)
type(uneven)
print(list(uneven))
?

What is printed when the code is executed?


Rewrite the lines of code in the above example, using list comprehensions
feet=[5, 46, 57.8, 4.6, 2564.846]
feet = ?
uneven = ?
type(uneven)
print(list(uneven))
Using list comprehension, implement the function coupled(list1, list 2), which takes in two lists
and returns a list that contains lists with the i-th elements of two sequences coupled together. You
can assume the lengths of two sequences are the same.
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> coupled(x, y)
[[1, 4], [2, 5], [3, 6]]
>>> a = ['c', 6]
>>> b = ['s', '1']
>>> coupled(a, b)
[['c', 's'], [6, '1']]

Das könnte Ihnen auch gefallen