CS602– Data-Driven Development with Python– Fall 2022 Midterm Exam
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CS602– Data-Driven Development with Python– Fall 2022
Problem 1
Part 1 (Multiple Choose) (10 Points)
Create a list that contains the odd integer values from 20 to 35 exclusive. Select all that are correct:
A. oddList = [j for j in range(20, 35) if j % 2 == 0]
B. oddList = [i for i in range(20, 36, 2)]
C. oddList = [z for z in range(20, 35) if z % 2 != 0]
D. oddList = [x for x in range(21, 35, 2)]
Part 2 (Multiple Choose) (10 Points)
Using the 2-dimensional list below provide ALL the possible addresses of the elements that contain the value 55.
lst = [[22, 55, 44, 66],
[ 1, 3, 5, 7],
[11, 33, 55, 77],
[ 2, 4, 6, 8]]
Part 3 (Write a Python statement) (10 points)
Write a list comprehension with the reference variable set to compList, that contains the value multiplied by the value. The input sequence equals the values: 2, 4, 6, 8, 10.
The resultant list will contain the values: [4, 16, 36, 64, 100].
Part 1 (Provide the output) (10 Points)
The following variable contains the following string:
myString = "catch drain apothem hatch"
Determine the values of the following expressions:
myString[:2]
myString[9:11]
myString[15:18]
myString[20:23]
Part 2 (Write code segment) (10 Points)
Using list addressing and/or sequence slicing take the following string and write a code segment that prints out the word python on a single line.
myString = "put your multi-threaded program on hold."
Part 3 (Write code segment) (5 Points)
Using f-string formatting construct a print statement that makes the output look exactly like what is printed below.
The name is printed in 20 spaces, left justified. (name = Joseph Smith)
The course code is printed in 11 spaces, centered. (course = CS602)
The score value is printed in 10 spaces left justified, with 2 digits to the right of the decimal point. (grade = 85.436767)
Problem 3
Part 1 (Provide the output) (5 Points)
Write the output of the following code segment:
for count in range(15, 1, -2):
print(count, end = " ")
Part 2 (Write code segment) (10 points)
Write a code segment that includes a while loop statement that walks a list (myList) that contains both positive and negative integers and prints out only the positive integers on a single line and are separated by a space.
myList = [23, -10, -22, 17, 44, -80, 0, 72]
Part 3 (Write code segment) (10 points)
Write a code segment that includes a for loop statement that walks a string (testString). The loop should evaluate each character in the string and prints out the ASCII code value for that character. If the loop encounters a space in the string, execute a newline (carriage return).
testString = "Python Program"
The output of the code segment should look like this:
P: ASCII Code: 80
y: ASCII Code: 121
t: ASCII Code: 116
h: ASCII Code: 104
o: ASCII Code: 111
n: ASCII Code: 110
P: ASCII Code: 80
r: ASCII Code: 114
o: ASCII Code: 111
g: ASCII Code: 103
r: ASCII Code: 114
a: ASCII Code: 97
m: ASCII Code: 109
Problem 4
(Write code segment) (20 points)
Complete the function below called countLetters(s). The s input parameter is a string data type. The string being passed to this function will only contain lowercase letters and spaces. The function should count occurrences of the characters a – z and print out the occurrence count value for each character found if the count is greater than 0 (zero). Do not count space characters. Use the starting code that is provided in the function below.
For the string example below, the following output would occur:
Example string only: 'boston is a great and historical city'
Letter - a: 4
Letter - b: 1
Letter - c: 2
Letter - d: 1
Letter - e: 1
Letter - g: 1
Letter - h: 1
Letter - i: 4
Letter - l: 1
Letter - n: 2
Letter - o: 3
Letter - r: 2
Letter - s: 3
Letter - t: 4
Letter - y: 1
Hints: Remove the spaces from the input string (s) before you process the letter counts.
def countLetters(s):
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# Place your code below to complete the function.
2022-11-28