CDS1001: Introduction to Programming for Data Science 1st TERM EXAMINATION, 2022-23
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CDS1001: Introduction to Programming for Data Science
1st TERM EXAMINATION, 2022-23
Section A (20%, 1% each)
1. Which are methods of list?
a. append()
b. extend()
c. insert()
d. All of the above
2. Which operator repeats a list for the given number of items.
a. *
b. +
c. &
d. None of the above
3. What does the term “Immutable” means?
a. They cannot be edited
b. They can be edited
c. Both of the above
d. None of the above
4. In python -1 corresponds to the:
a. Last index
b. First index
c. Middle index
d. End index
5. Empty list in python is made by?
a. l = []
b. l = list()
c. Both of the above
d. None of the above
6. Consider the program below:
|
word = “Python Programming” n = len(word) word1 = word.upper() word2 = word.lower() converted word = “” for i in range(n): if i % 2 == 0: converted_word += word2[i] else: converted_word += word1[i] print(converted_word) |
a. Python Programming
b. pYtHoN PrOgRaMmInG
c. python programming
d. PYTHON PROGRAMMING
7. Which of the following statements is correct?
a. List is mutable and Tuple is immutable
b. List is immutable and Tuple is mutable
c. Both list and tuple are mutable.
d. Both list and Tuple are immutable.
8. To insert 5 to the third position in list1, which of the following statements should we use?
a. list1.insert(3,5)
b. list1.insert(2,5)
c. list1.add(3,5)
d. list1.append(3,5)
9. Which of the following is NOT an advantage of using modules?
a. Reuse of program code.
b. Dividing up tasks
c. Reduce the size of the program
d. Testing individual parts of a program
10. Which of the following is false regarding loops in Python?
a. Loops are used to perform certain task repeatedly.
b. While loop is used when multiple statements are to be executed
c. While loop is used when multiple statements are to be executed repeatedly until given condition become True.
d. For loop can be used to iterate through the element of lists.
11. What does an assignment operator do?
a. It represents an algebraic equation that the computer solves.
b. It causes the computer to store the result of the statement’s expression in memory so you can use the result in a later Python statement.
c. It outputs the result of the statement’s expression to the screen.
d. It asks the user to enter a value for the assignment’s expression.
12. What data type does the string’s split method return?
a. A string.
b. A list of strings.
c. An int.
d. None.
13. Which of the following is a reason to use functions?
a. To avoid duplicating code.
b. To make the code more organized.
c. To make it easier to read by keeping sections of code shorter.
d. All of the above
14. When the execution of a function ends, what Python code is executed next?
a. The line of code immediately after the end of the function.
b. The code immediately after where the function was called from.
c. The program ends without executing any more code.
d. The function is automatically executed again.
15. What variable names does a function have access to?
a. All variables used in a program.
b. Only variables created in the function.
c. Only the parameters passed to the function.
d. Parameters that are passed to the function and any variables created in the function or outside the function.
16. When a function returns a value, the options for the caller are to
a. Ignore the return value
b. Use the result as part of an expression (e.g., x = y + math.sqrt(5))
c. Assign it to a variable (e.g., x = math.sqrt(5))
d. All of the above
17. What is the purpose of exception handling?
a. To handle errors gracefully so the program does not crash.
b. To make a program crash.
c. To make algorithms more efficient.
d. None of the above
18. A while loop written as while True
a. will always be an infinite loop no matter what the loop body contains
b. will never be an infinite loop no matter what the loop body contains
c. might be an infinite loop depending on what the loop body contains
d. will never execute the loop body
19. When you iterate over a dictionary, what value is assigned to the loop variable?
a. Each key in the dictionary.
b. Each value in the dictionary.
c. A tuple containing a key and value pair.
d. None of the above
20. Which of the following statements is true about the Python expression x == 3 or 4?
a. It is an invalid statement, and the Python interpreter will indicate it is a syntax error.
b. It will be true if x is 3 or if x is 4, and false for any other values.
c. It will always be false.
d. It will always be true.
Section B (80%)
Q1: Write a program that read two positive odd integers as the number of rows and columns to draw the American flag in different sizes.
If the input numbers are invalid (the number of rows and columns must be positive and odd), please ask the user inputs again. You are allowed to print a character at a time. (10 marks)
Example outputs:
|
Rows:1 Cols:1 * |
Rows:9 Cols:11 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
Rows:11 Cols:9 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
Rows:3 Cols:3 * * * * * |
||
|
Rows:5 Cols:5 * * * * * * * * * * * * * |
Q2: Given a list of two integers and a target number (an integer). You need to write a function findExpression(nums, target) to find an expression string that use two integer numbers only once along with an operator from [+,-,*,/] to produce the result which is equivalent to the target. (15 marks)
Example outputs:
Example 1: findExpression([2,3], 6) should output:
2 * 3 = 6
3 * 2 = 6
Example 2: findExpression([1,2], 2) should output:
1 * 2 = 2
2 * 1 = 2
2 / 1 = 2
Q3: (30 marks)
a) We need to write a function extractRIs() to extract the names of the academic staff along with their research interests from the given txt file. (10 marks)
Note that in this question we assume that research interests appear in the next line of the email
The function should return the output as follows:
b) Make use of the function you created in part a) to count the occurrence of each
research interest, sort the research interests in decreasing order by the number of occurrence and print out the top 10 research interests with the most occurrences (10 marks)
c) Revise your code in part b and use the matplotlib package to plot a bar chart of top 10 popular academic staff's research interest, where the x-axis represents the abbreviation of the research interest and the y-axis represents the number of staff has that research interest. (5 marks)
Suppose the top 10 research interests are the follow:
• Supply Chain Management
• Operations Management
• Quality Management
• Maritime Economics
• Logistics and Supply Chain Management
• Transportation
• Transport Economics
• Socially Responsible Operations
• Smart City Operations
• Operations and Marketing Interface
The abbreviation should be taking the initial character for each word in the research interests, such that Supply Chain Management will become SCM, Operations Management become OM, etc.
d) Export to Excel file (5 marks)
use the package openpyxl so as to create an Excel file named research_interests.xlsx in the report folder. The excel file contains one worksheet with a title 'Research Interests'. The worksheet has two columns, with the first column named 'Research Interests' showing each author, and with the second column named 'Total' showing the total number of staff have this research interest. The first column has a width of 46, and the second column has a width of 10.
The first row is the header showing the names of columns in bold. Data of the remaining rows are ordered by non-decreasing order of the second column, and breaking ties by alphabetical order of string values of the first column (if values of the second column are the same).
Sample Output:
Q4: In this question, you are required to develop a program that can convert the plain text message to the ciphertext by using keyword-based transposition cipher.
In the keyword-based transposition cipher, we have a key and a plaintext message.
Suppose our plaintext message is
|
BUY SOME MILK AND EGGS |
|
MYMONEY |
. (25 marks)
Task 1 (10 marks)
First, we need to obtain the order from the key (based on alphabetic order) (Task 1) For example if we have the key MYMONEY, we need to obtain the order from the key
• Based on alphabetic order
• If a character has multiple occurrences, we sort the character from left to right
• e.g. In the table below, the character comes first in the alphabetic order is 'E', so its rank = 1.
As we have two occurrences of 'M', we sort them from left to right.
o Since 'M' comes after 'E' in the alphabetic order, the first 'M' in the 1st position will have the rank of 2
o The second 'M' in the 3rd position will have the rank of 3.
|
M |
Y |
M |
O |
N |
E |
Y |
|
2 |
6 |
3 |
5 |
4 |
1 |
7 |
Task 2 (5 marks)
Since the key MYMONEY has the length of 7, then we re-construct the message with the width of 7, the message should look like this:
|
B |
U |
Y |
|
S |
O |
M |
|
E |
|
M |
I |
L |
K |
|
|
A |
N |
D |
|
E |
G |
G |
|
S |
|
|
|
|
|
|
Task 3 (10 marks)
Then, we can follow the order we obtained in the key to output the message in the vertical manner. Since the second last column (MYMON
E
Y) has the highest priority, so we output the second last column: OKG, then the next one should be the first column (
M
YMONEY)
hence it is BEAS. After the whole process, the ciphertext will be:
|
OKGBEASYMDSLE I U_NM_G |
|
#Task 1 (10 marks) def calRank(key, pos):
#Task 2 (5 marks) def printMessageInBlocks(key, plaintext):
#Task 3 (10 marks) def encryptMessage(key, plaintext): |
2024-12-24