This is a set of exercise problems for the midterm. A set of exemplary answers will be posted on Tuesday 18:00 pm.
1 2
1 2 3
1 2 3 4 5 6 7
1 2 3 4 5 6 7
Name: Student ID: Class: Instructor: Jong-Kyou Kim, PhD
1. The following is a Python program and its output. Fill in the blank.
>>> print([_______ for x in range(5)])
[1, 3, 5, 7, 9]
2. Show the output of the following program
>>> list=[0,1,2,3,4,5]
>>> print(list[_______])
[5, 3, 1]
3. Show the output of the following program
>>> k = 0
>>> for i in range(100): … k=k+1
… for j in range(200): … k=k+1
>>> print(k)
4. The following is a Python program and its output. Fill in the blank.
CS2704 Data Analytics using Python – Midterm exercise 2020-06-08
>>>
>>>
…
…
k=0
for i in range(100):
for j in range(___): k=k+1
>>> 20000
print(k)
5. Define the function f(n) to compute factorial, e.g., f(3) = 3! = 3 × 2 × 1. When imple- menting the algorithm, use only the recursion, i.e., do not use iterations.
CS2704 Data Analytics using Python – Midterm exercise, Page 2 / 2 2020-06-08
1 def f(n):
2 +—————————————————————+ 3|| 4|| 5|| 6||
7 +—————————————————————+
8 >>> print(f(5))
9 120
6. Thefunctionmysplitsplitsastringintowordsandreturnsalistofstringseparatedbyspace character. For example
1 >>> s = ’This is right justified’
2 >>> print(mysplit(s))
3 [’This’, ’is’, ’right’, ’justified’]
Complete the following function
1 def mysplit(s):
2 +—————————————————————+
3|| 4|| 5|| 6|| 7|| 8|| 9||
10 | |
11 | |
12 | |
13 | |
14 | |
15 +—————————————————————+