You are on page 1of 5

CMP4266 Week 4 Lab

Essential - to be completed within the week


All essential tasks should be should submitted to CodeGrade - Lab4.

You can submit all tasks at once or just the script for one task.

1. Manipulating a list
Reference: Lecture Note 7-17
Estimated time of Completion: 10-15 minutes

1. Create a script named lab4_list.py.


2. Before attempting this task, make sure you have tried all the codes from the lecture notes if you
are new to Python programming.
3. In the script, create the following functions:

Function Parameters What does it do?

create_list N/A It returns a list with the following elements:


['PlayStation', 'Xbox', 'Steam', 'iOS', 'Google Play']

get_info my_list - It returns the following information of my_list as a tuple:


list type the first element, the second last element, number of elements.

get_partial my_list - It returns a new list which contains the 2nd, 3rd, and 4th elements
list type from my_list, in the original order.

get_last_three my_list - It returns a new list which contains the last three elements from
list type my_list, in the reversed order.

double_list my_list - It returns a new list which concatenates two of my_list.


list type

amend my_list - It returns a new list which the following amendments:


list type change the 2nd element of my_ list to "None",
add "Bye" to the end of my_list.

4. You can check your solution with the follow codes before submitting to CodeGrade:
if __name__ == "__main__":
test_list = create_list()
print(test_list)
print(get_info(test_list))
print(get_partial(test_list))
print(get_last_three(test_list))
print(double(test_list))
print(amend(test_list))

2. Times table
Reference: Lecture Note 31-42
Estimated time of Completion: 20-30 minutes

1. Create a script named lab4_times_table.py.


2. In the script, create a function named times_table1(x), which takes an integer parameter.
3. This function prints out the times table of the given number.
4. In terms of the format, times_table1(8) prints out:

5. Next, create another function named times_table2(x), which takes an integer parameter.
6. This function prints out the full times table till the number x.
7. For example, times_table2(3) prints out:

8. Note: the width of each integer is set to 4.

3. Half pyramid
Reference: Lecture Note 34-42
Estimated time of Completion: 10-15 minutes

1. Create a script named lab4_pyramid.py.


2. In the script, create a function named print_half_pyramid(level), which takes an integer
parameter (must be greater than 0, i.e., minimum is 1).
3. This function prints out a half pyramid with given levels (below level = 10):

4. Note: There is a space in between "*".

4. Prime number
Estimated time of Completion: 15-20 minutes

1. Create a script named lab4_prime.py.


2. In the script, create a function is_prime(num), which returns True or False depends on whether
its parameter (integer type) is a prime number or a composite number.
a. For all numbers small than 2, it always returns False.
b. 3 is a prime number so is_prime(3) returns True.
c. 4 is a composite number so is_prime(4) returns False.
d. Need some help with prime number? Check here: What is prime number.
3. Hint. To check whether X can be divided by Y, you can use the mod operator %, which you used in
Lab 3. If X % Y equals to 0, that means X can be divided Y.
a. For example, 4 % 2 = 0. This means 4 is not a prime number as it can be divided by 2.
4. Next, create another function list_prime(num), which prints all the prime numbers equal or
smaller than the parameter (integer type).
5. In terms of the format, list_prime(13) prints out: .

5. A moving robot
Estimated time of Completion: 40-50 minutes

You are going to design a program which allows a robot to move in a 2D coordinate system starting from the
original point (0, 0). The robot can move to the next point (x, y) specified by the user. If the next point is
(999, 999), the program ends and prints out the distance moved at each step.

Your program (lab4_robot.py) can be implemented in various ways as long as it has the below
behaviour/display format - user inputs are highlighted in yellow:

However, your program must contain at least the following functions:

Function Parameters What does it do?

get_next_point step It returns a tuple (x, y) from the user input. The parameter step
- int type indicates the step number.
For example:
get_next_point(3) asks input for x3, y3 (user input in yellow)

It returns (3, 5) - note the return is a tuple and has no step info.

cal_distance p1, p2 It returns the distance (float) between the two points.
- tuple type To calculate the distance between two points (x1, y2) and (x2,
y2), use the formula below:

How to calculate the square root? Refer to Lab 1 - task 10.


main N/A It runs the whole robot program.
It uses get_next_point() to take user inputs.
It uses cal_distance() to calculate the distance between two
points.
It could use any other functions created by you as well.

Further notes on output format:

• "------ Robot Program ------" has 6 "-" at each end.


• " ----------" before and after the steps has 10 "-".
• The units should be printed with 2 decimal places.

Use the following code to test your robot program:

if __name__ == "__main__":
main()
Advanced - challenge yourself after completing all essential tasks
You need to think harder and sometimes with a bit research

6. Full pyramid
1. In script lab4_pyramid.py, create another function named print_full_pyramid(level,
symbol), which takes two parameters: level and symbol.
• level: an integer and it's greater than 0.
• symbol: a string to form the pyramid.
2. See blow for the print format of the pyramid:
print_full_pyramid(6,"*") print_full_pyramid(6,"#") print_full_pyramid(9,"Hi")

print_full_pyramid(4, "It's hard")

You might also like