1.1 Python en
1.1 Python en
30 November 2020
Instructions
1. Do this module according to the tested material (Function, Procedure, and Matrix). Don’t use other
materials that have not been tested.
2. Pay attention to the file extension (*.py). Source code without extension may not be graded.
# SID/Name :
# Date :
# Description :
5. All source code should be compressed with filename format P04 SID.zip.
6. You may assume user inputs are valid, except if it is explicitly stated in the statement.
7. It will be better if your code has good indentation and comments (usage of variable, control, loop,
function, or procedure), so that the debugging process will be easier.
8. Cheating (copying and pasting) from other participant(s) or other source(s) will not be tolerated and you
will get a serious penalty.
9. If there are any differences between the module’s instructions and your assistant’s instructions, please
follow your assistant’s.
10. Good luck!
1
Problem 1
Mr. Qu has two functions, which are f (x) = 2x + 3 and g(x) = 4x − 5. Your job is to make a program that finds
an integer x between l and r such that the absolute difference of f (x) and g(x) is as small as possible.
Make two functions, each for finding the value of f (x) and g(x).
Example 1
Input l: -10
Input r: 10
The integer x found is 4.
Example 2
Input l: 6
Input r: 8
The integer x found is 6.
2
Problem 2
Mr. Qu likes to play with integers. He has got an idea to multiply all the digits of the integer in his hand, and
change the integer into the result of that multiplication. This digit multiplication process will not stop until the
number of digit in his hand equals to 1.
After doing several operations, he wants you to make a program that can do it for him. Make a program that
accepts a starting integer, and repeats the process until the number of remaining digits is 1. Use a function to
simulate the process of changing the integer into the digit multiplication.
Example 1
Input the initial integer : 539
After 1 process : 135
After 2 process : 15
After 3 process : 5
Example 2
Input the initial integer : 537
After 1 process : 105
After 2 process : 0
3
Problem 3
Mr. Kan is learning about matrices. He currently knows about a square matrix, a matrix which has the same
number of rows and columns. Other than square matrix, there are several special matrices, which are described
as follows:
• Upper triangle matrix: Matrix whose all elementts below its main diagonal is ’0’.
• Lower triangle matrix: Matrix whose all elementts above its main diagonal is ’0’.
• Diagonal matrix: Matrix whose all elementts above and below its main diagonal is ’0’. In other words,
the combination of upper triangle matrix and lower triangle matrix.
Help Mr. Kan to make a program that accepts a matrix and prints what type of matrix is it (upper triangle
matrix, lower triangle matrix, diagonal matrix or neither of them).
Example 1
Input n: 3
Input element 1 1: 1
Input element 1 2: 2
Input element 1 3: 3
Input element 2 1: 0
Input element 2 2: 4
Input element 2 3: 5
Input element 3 1: 0
Input element 3 2: 0
Input element 3 3: 0
Upper triangle matrix .
Example 2
Input n: 2
Input element 1 1: 1
Input element 1 2: 0
Input element 2 1: 0
Input element 2 2: 4
Diagonal matrix .
Example 3
Input n: 2
Input element 1 1: 1
Input element 1 2: 1
Input element 2 1: 1
Input element 2 2: 1
Not a special matrix .
4
11
11