9569 H2 Computing
Topical Exercises #3 (Python Basics: Tuples, Lists)
Exercise 1: Estimation of π
One computation technique for estimating the value of π is the
Monte Carlo method, which relies on repeated random sampling.
The idea is to simulate random points within a square of sides 2 units
centred at the origin. Consider also a circle with radius 1, centred at
origin. By considering the proportion of these generated points that
lie inside the circle, we can estimate π.
Write a function rand_pt() that returns a tuple (x, y) representing
a random point in the square. You may use random.uniform(-1, 1) to
generate a random number between -1 and 1.
Write another function is_in_circle(point) that takes in a tuple and returns True if is inside
the circle, and False if otherwise.
Finally, write a function pi_estimate(n) that makes use of the previous two functions to
generate n points in the square, test whether each is in a circle, and returns an approximate value
of π based on the simulation outcome.
Test and show the output for n = 105 and n = 107.
Exercise 2: Lucky numbers
This is a game of luck. The user has 5 attempts to guess the 5 lucky numbers between 1 and 20
inclusive. He gets $20 for every correct guess.
Write a program that:
• creates a tuple of 5 distinct random numbers between 1 and 20 inclusive (here, you are
restricted to use only the randint method in the random module)
• requests numeric input from the user for 5 times, without revealing whether the guess is correct
• prints the amount of money that the user receives from all his guesses at the end
Exercise 3.1: 3D vectors
The task is to perform various operations on 3D vectors represented by tuples (x, y, z).
Write the following functions:
• magnitude(v1), which calculates and returns the magnitude of the vector v1.
• dot(v1,v2), which calculates and returns the dot (scalar) product of v1 and v2.
• cross(v1,v2), which calculates and returns a tuple representing the cross product v1 x v2.
Exercise 3.2: Vector calculations
With the use of the above, write the following functions:
• angle(v1,v2), which returns the angle between the two vectors
• proj(v1,v2), which returns the length of projection of v1 on v2.
• triangle_area(v1,v2), which returns the area of a triangle OAB with sides OA and OB
represented by the vectors v1 and v2 respectively.
Show the output of the three functions using v1 = (1, 2, 3) and v2 = (4, 5, 6).
Exercise 4: Finding the top 3
Write program code that prints the top 3 largest elements within a given list.
Test with the list [15, 20, 1, 7, 9, 4, 8, 11, 19, 3, 14, 10] and show the output.
Exercise 5.1: List reversal without built-in methods
In this task, write a procedure reverse_list(input_list) that takes a list as input and
reverses that list. Test with the list [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’].
Do NOT use built-in list methods such as reverse() or slicing ([::-1]) to tackle this task.
Exercise 5.2: Union and Intersection
Given list1 = [1, 2, 3, 4, 5] and list2 = [3, 5, 7, 9],
the union of the two lists is [1, 2, 3, 4, 5, 7, 9] and the intersection is [3, 5].
In Python, we could possibly obtain union and intersection of two lists using set operations or list
comprehensions.
Example: Intersection using sets
intersect_set = set(list1).intersection(set(list2))
intersect_list = list(union_set)
Example: Intersection using list comprehension
intersect_list = [item for item in list1 if item in list2]
However, now we want to write the functions get_intersection(list1, list2) and
get_union(list1, list2) WITHOUT the use of these techniques. Write program code for
each of these functions and print the output given the two lists [‘apple’, ‘banana’, ‘cherry’] and
[‘banana’, ‘durian’, ‘cherry’, ‘peach’].
Exercise 6: Student assessment records
The task is to create a program that manages student assessment records. Each student has a
name, a list of marks for continual assessments (CA) and a single mark for the final exam. The
overall mark for a student is calculated by taking 30% of the average CA marks and 70% of the
final exam. This program should print the following information:
• the overall mark for each student
• the students who did not pass (i.e. less than 50 marks overall)
The student data is in this format:
student_data = [[“Ben”, [95, 88, 79], 87],
[“Jerry”, [62, 45, 70], 68],
# ... other students (add at least 2 more entries)
]
Display the output of your program. A sample output is:
Ben: Overall = 87.1
Jerry: Overall = 65.3
Tom: Overall = 49.6
Mary: Overall = 75.7
Students Who Did Not Pass: Tom