Professional Documents
Culture Documents
ENGG1330 2N Computer Programming I (20-21 Semester 2) Assignment 1
ENGG1330 2N Computer Programming I (20-21 Semester 2) Assignment 1
ENGG1330 2N Computer Programming I (20-21 Semester 2) Assignment 1
Assignment 1
Due date: 17-April-2021, 23:59. Late submission: 10% deduction per day.
$5 $2
Peter got a basket of eggs from his friend several months before, now his farm can
produce eggs daily and sell to other villagers. Since the sizes of eggs are different,
Peter plans to divide the eggs into two categories (large and small) and sell them at
different prices. Being an honest man, he wants the sizes of eggs under the same price
should be as close as possible. He defines the variance each category as below:
𝑛𝑐
𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒𝑐 = ∑𝑖=1(𝑆𝑖 − 𝑎𝑣𝑒𝑟𝑎𝑔𝑒𝑆𝑖𝑧𝑒𝑐 )2
where:
nc is the number of eggs in the category C
Si is the size of ith egg in the category C
averageSizec is the average size of eggs in the category C
Therefore, the variance of the small and large categories should be:
𝑛𝑠𝑚𝑎𝑙𝑙
𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒𝑠𝑚𝑎𝑙𝑙 = ∑𝑖=1 (𝑆𝑖 − 𝑎𝑣𝑒𝑟𝑎𝑔𝑒𝑆𝑖𝑧𝑒𝑠𝑚𝑎𝑙𝑙 )2
𝑛
𝑙𝑎𝑟𝑔𝑒
𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒𝑙𝑎𝑟𝑔𝑒 = ∑𝑖=1 (𝑆𝑖 − 𝑎𝑣𝑒𝑟𝑎𝑔𝑒𝑆𝑖𝑧𝑒𝑙𝑎𝑟𝑔𝑒 )2
In other words, the variance of a given category is obtained by adding the square of
the difference between each egg and the average size of eggs in the category.
For example, the following are the distribution of eggs in each category and their
corresponding variances and average sizes.
1
The total variance of the above distribution is 12.0. If move the last egg (size=5) from
the small category to the large category, the total variance will become 10.00, and the
average size and variance of each category will be:
2 5 4 9 3 5 6 8 7 9 10 50.40
3 12 13 15 9 8 33.20
4 3 4 3 4 5 6 2 4 3 11.56
5 10 11 9 12 8 10 11 12 11 9 16.10
Input: Two lines of string, each string contains indefinite number of integers,
separated by a space, terminated by a carriage return (linefeed) character.
2
2 1 4 5 6 7 15.00
2 3 8 9
3 8 9 10 11 11 12 4.00
9 10 11 12
4 12 13 5.17
15 9 8
Output:
• The minimal variance, in 2 decimal places, terminated by a carriage
return (linefeed) character.
• The distribution of eggs in each category. Eggs are printed in ascending
order within the category. Categories with lesser elements will be
printed first. The output format of category is [ + size + space + size +
space + … + size + space+].
• In case there are more than one distribution which could give the
minimal variance, output all distributions, sorted by size of categories.
• Eggs with same size should be grouped into same category as far as
possible as they will be sold at the same price. For example, both [1 ][1
] and [][1 1 ] give 0 variance and only [][1 1 ] should be outputted.
• Distribution is terminated by a carriage return (linefeed) character.
You may refer to the following link for sorting a list in Python,
https://www.w3schools.com/python/ref_list_sort.asp
2 1 6 4 5 7 2 8 9 3 15.00
[1 2 3 4 ][5 6 7 8 9 ]
[1 2 3 4 5 ][6 7 8 9 ]
3 8 9 10 11 11 12 9 10 11 4.00
12 [8 9 9 10 10 ][11 11 11 12 12 ]
4 8 9 12 13 15 5.17
3
[8 9 ][12 13 15 ]
2 2 15.00
1 2 3 4 5 6 7 8 9 [1 2 3 4 ][5 6 7 8 9 ]
[1 2 3 4 5 ][6 7 8 9 ]
3 1 60.00
1 2 3 4 5 6 7 8 9 [1 2 3 4 5 6 7 8 9 ]
4 5 2.00
1 2 3 4 5 6 7 8 9 [1 ][2 3 ][4 5 ][6 7 ][8 9 ]
[1 2 ][3 ][4 5 ][6 7 ][8 9 ]
[1 2 ][3 4 ][5 ][6 7 ][8 9 ]
[1 2 ][3 4 ][5 6 ][7 ][8 9 ]
[1 2 ][3 4 ][5 6 ][7 8 ][9 ]
5 5 0.00
1 1 1 1 1 [][][][][1 1 1 1 1 ]
4
Submission
Virtual Programming Lab (VPL) will be setup for testing and submission. Only the last
submission will be counted as the final. Please test your program thoroughly before the
deadline. Your program has to generate the output according to the given specifications,
i.e., without extra text/space. The test cases in VPLs only test the basic requirement, we
may mark your program with another test cases.
Program should only use the Python libraries/features covered in this course. Using of 3nd
party library is not allowed.
10% of marks will be deducted from the final mark for every 24 hours after the submission
due date. The penalty will only apply to the late submitted part.
Please do not submit any program after the due date if you work is final. Any submission
after due date is regarded as late submission.
Plagiarism
Plagiarism (detected by the system) will get zero mark, which apply to the source provider(s)
as well. In other words, students who submit same/highly similar programs will all get zero
mark. Student has full responsibility to protect his/her program from being accessed by
others.