0% found this document useful (0 votes)
25 views5 pages

1.1 Python en

The document provides instructions for Practical Session #4 on functions, procedures, and matrices in Python. It outlines 3 problems to solve involving functions to calculate values from other functions, a procedure to repeatedly multiply the digits of an integer, and a program to determine if a matrix is upper triangular, lower triangular, diagonal, or not a special matrix. The problems are to be saved as Python files with a specific naming convention and include comments, indentation, and the student's information. Plagiarism is prohibited.

Uploaded by

key
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

1.1 Python en

The document provides instructions for Practical Session #4 on functions, procedures, and matrices in Python. It outlines 3 problems to solve involving functions to calculate values from other functions, a procedure to repeatedly multiply the digits of an integer, and a program to determine if a matrix is upper triangular, lower triangular, diagonal, or not a special matrix. The problems are to be saved as Python files with a specific naming convention and include comments, indentation, and the student's information. Plagiarism is prohibited.

Uploaded by

key
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical Session #4

Function, Procedure, and Matrix

Tim Materi Pengenalan Komputasi 2020/2021

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.

3. Make sure your program can be compiled and run.


4. For every source code, provide your identity, at least:

# 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

Save the file as: P04 NIM 01.py.

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

Save the file as: P04 NIM 02.py.

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

Save the file as: P04 NIM 03.py.

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 .

Explanation: The matrix is


123
045
000

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 .

Explanation: The matrix is


10
04

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 .

Explanation: The matrix is

4
11
11

You might also like