COMP3010 Lab03 newQ2Testcases
COMP3010 Lab03 newQ2Testcases
• This is an individual assessment and will typically be released at the start of the weekly
lab session.
• Your program should work correctly on all inputs. If there are any specifications about
how the program should be written (or how the output should appear), those specifications
should be followed.
• Your code and functions/modules should be appropriately commented. Variables and
functions should have meaningful names, and code should be organized into objects,
functions, or methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You should NOT copy
or share your code with other students to avoid plagiarism issues.
• Your answers for the Theory questions should be in PDF (.pdf) format. It can be hand-
written or typed using any program and format, as long as we can read and understand
your answer. The file name should follow the format: ID_WeekNumber_Theory.pdf, e.g.
V02023001_Week1_Theory.pdf
• Your answers for the Programming questions should be in Python script (.py) format.
The file names should follow the format: ID_WeekNumber_Programming_QNumber.py, e.g.
V02023001_Week1_Programming_Q2.py
• You should upload your Theory questions PDF to Canvas, and Python files for Program-
ming problems to both Canvas and CMS. You must submit all files before the end of
Friday unless the instructor gave a specified deadline.
• Late submission of weekly homework without an approved extension will incur the following
penalties:
(a) The instructor will deduct (1) point for each business day past the deadline.
(b) The penalty will be deducted until the maximum possible score for the homework
reaches zero (0%) unless otherwise specified by the instructor.
Question 1 (5 pts)
As a third year computer science student, you believe you have a good grasp of various pro-
gramming languages, algorithms, and data structures. Due to this confidence, you decide to
visit a first year class, review the code written by freshmen, and provide suggestions on how
to improve their efficiency. On a particular day, you encounter a student with this small code
segment:
You immediately noticed that this is not good at all. You need to analyse its complexity to
persuade that student to do some refactoring. What is the complexity of above code segment?
Explain your solution.
Question 2 (4 pts)
Until now, you have seen the notations Big O for upper bound and Big Ω(mega) (Knuth’s com-
plexity theory) for lower bound analysis. These are sets of functions, which are especially useful
to analyse the approximation of a given function using finite series (e.g. Taylor’s series). This
is conveniently applicable to computer science to analyse algorithms. Another member of the
Bachmann–Landau notations family is Big Theta Θ, for the tight bound (both upper and lower)
analysis.
Let’s review their formal definitions:
Big O notation: f (n) = O(g(n)) such that ∃k > 0∃n0 ∀n > n0 : |f (n)| ≤ kg(n) tells us that |f | is
asymptotically bounded above by a constant factor of g.
Big Omega notation: f (n) = Ω(g(n)) such that ∃k > 0∃n0 ∀n > n0 : f (n) ≥ kg(n) tells us that f
is asymptotically bounded below by g.
Another useful notation is Big Theta notation: f (n) = Θ(g(n)) such that ∃k1 > 0∃k2 > 0∃n0 ∀n >
n0 : k1 g(n) ≤ f (n) ≤ k2 g(n) which tells us that f is asymptotically bounded both above and
below by g.
f1 + f2 = Θ(max{f1 , f2 })
In addition, think about the opposite operator, let assume the subtraction f1 − f2 also produce
a asymptotically positive function, prove or disprove the following statement:
max{f1 , f2 } = Θ(f1 − f 2)
Question 3 (3 pts)
When developing software, especially ones that have to deal with data, you will often come
across duplicate entries that are unwanted. You need to design an algorithm that take a se-
quence of n integers and remove all elements that appear more than once. Your solution must
be efficient enough to run reliably on microprocessors with limited memory.
Question 4 (2 pts)
One common method of speeding up sorting in practice is to sort using a fast sorting algorithm
like Quicksort or Mergesort until the subproblem sizes are small and then to change to using
insertion sort since insertion sort is fast for small, nearly sorted lists. Suppose we perform
Mergesort until the subproblems have size k, at which point we finish with insertion sort. What
is the worst-case running time of this algorithm?
As a junior computational analyst, you are tasked to analyse and implement new numerical
methods to produce and process large scale simulations. Your first task is to analyse the amount
of memory required by an exisitng simulation. After some test runs, you found the relationship
between the input n for the simulation and the amount of memory consumed to follow a harmonic
relationship. The harmonic numbers are defined as the partial sums of the harmonic sequence:
n
X 1
H(n) :=
i
i=1
Problem 1 (5 pts)
You are on a team building trip with your classmates. The professor prepared a game for you,
so that you can also learn while you play. Professor K first make everyone cover their eyes, and
then randomly choose N individuals and make them form a line. They will serve as the problem,
and the remaining students would be the players. A player will have only one turn go through
all the people in the line sequentially and ask them their height, and pick out the couple
has the greatest height difference. You win the game if you can provide Prof. K with correct
maximum height difference. But Prof. K wanted to add a twist, the pair that you pick must have
the shorter student have to stand before the taller in the line (if the pair you pick have the taller
person appear earlier in the line than the shorter one, it does not count). As an avid competitive
programmer, you try to solve the game with Python implementation. If no such couple exists,
return -1.
Here is an example for an array of height: [2, 3, 10, 6, 4, 8, 1], you should print 8, which
corresponds to pair (2,10). In this case, the pair (10,1) does not count because 10 appeared
before 1 in the line.
The program input is a line of space-separated integers for the height of people in the line.
Sample output:
Problem 2 (4 pts)
In a bustling kitchen, a group of bakers were competing in a giant cake challenge hosted by the
infamous Rordon Gamsey. The competition was fierce, and tensions were high as the bakers
raced to gather the ingredients they needed to make the ultimate cake.
One of the key ingredients was dough, and the bakers had to find pairs of dough bags whose
combined weight can be baked into multiple k-kilogram cakes with no wastes. To minimize
waste and avoid the wrath of Rordon Gamsey, they had to be careful not to pick bags with more
dough than necessary or end up with any leftover. Each baker must get exactly 2 bags of dough
because the chef said so and no one can argue.
To help them in their quest, you turned to your trusty CS degree and wrote a Python program to
find all the unique pairs of dough bags whose weight added up to factors of k kilograms. As the
competition heated up, your programs were put to the test with larger and larger lists of dough
bags, but they were able to keep up with ease thanks to your efficient algorithms.
But chef Rordon Gamsey wanted to know the total amount of dough in all the pairs of bags that
For example, given the list [2, 5, 7, 3, 4, 8] and k=6, the pairs are (5, 7), (2, 4), and (4,
8) and their sum is 12, 6, and 12 respectively. The final output would be the sum of all these
values which is 30.
Your program should read a list of space-separated integers from user input, and an integer k.
Your program should still work even if the list contains negative integers. You can assume that
k > 1. Your program should scale well with large input size. For this example, the input for your
program will look like:
Problem 3 (2 pts)
Imagine you are a farmer, who makes a living off of raising and selling cattles. As you have thirty
years working on this, you are smart enough to know you should sell neither the biggest pig nor
the smallest one. Instead, the k-th smallest one should be chosen for sale. In case multiple
pigs have the same weight, they are still considered having consecutive ranks. Please provide
the optimal solution for finding that pig, given an array containing all the weight of your pigs.
Here is an example for the array of weights: [7, 10, 4, 3, 20, 15], and k=2, you should
output 4, which correspond to the weight of 2-th (second) smallest pig.
Input format:
Sample output:
======================================================
Problem 1:
======================================================
=================
Case 1:
57 17 34 51 91 34 31 59
== Output ==
74
=================
Case 2:
52 42 33 100 31 81 84 13
== Output ==
67
=================
Case 3:
10 30 75 15 44 99 15
== Output ==
89
=================
Case 4:
100 9 22 9 66 51 77 62 33 70 3 65 75 49 79 5 53 96
== Output ==
93
=================
Case 5:
21 82 22 90 94 78 65 49 45
== Output ==
73
== Output ==
-114
=================
Case 2:
71 65 34
25
== Output ==
0
=================
Case 3:
== Output ==
0
=================
Case 6:
70 94 62 -18 -42 93 68 70 56 -64 10
16
== Output ==
48
======================================================
Problem 3:
======================================================
=================
Case 1:
== Output ==
55
=================
Case 2:
79 59 59 67 82 66 35 66 24 33 16 4 29 44
7
== Output ==
44
=================
Case 3:
37 28 85 18 58 46 82 43 36 74 28 38
8
== Output ==
46
=================
Case 4:
77 69 42 72 42 77 83 25 20 26 14
3
== Output ==
25
=================
Case 5:
17 2 80 83 20 38 78 6 10 13 8 66 23 47 70 27 3 19 9 86
18
== Output ==
80