ENGG1330 2N Computer Programming I (20-21 Semester 2) Assignment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ENGG1330 2N Computer Programming I (20-21 Semester 2)

Assignment 1

Due date: 17-April-2021, 23:59. Late submission: 10% deduction per day.

My egg is the best for Egg Yolk Misozuke

$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.

Category Distribution Average Size Variance


small 1, 2, 3, 4, 5 3.00 10.00
large 6, 7, 8 7.00 2.00

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:

Category Distribution Average Size Variance


small 1, 2, 3, 4 2.5 5.00
large 5, 6, 7, 8 6.5 5.00
The above arrangement is more preferable as the total variance is smaller.

A. Level One (20%)


Knowing that you are excellent in programming, Peter asked you a favor to
write a computer program to calculate the variance of a given set of eggs.

input: A list of eggs' sizes in integer, separated by a space, terminated by a


carriage return (linefeed) character.

Output: Variance of the category, in 2 decimal places, terminated by a carriage


return (linefeed) character.

Case Sample Input Sample Output


1 10 20 30 200.00

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

B. Level Two (30%)


With your help, Peter found that his clustering of eggs is not always perfect.
You suggested using a computer program to optimize the eggs distribution.
The program reads two lines of string. Each string represents the sizes of eggs
in a category. It may reallocate eggs to the other category so that the total
variance is minimal. The number of reallocations should be kept in minimal.

Input: Two lines of string, each string contains indefinite number of integers,
separated by a space, terminated by a carriage return (linefeed) character.

Output: The minimal variance, in 2 decimal places, terminated by a carriage


return (linefeed) character.

Case Sample Input Sample Output


1 1 2 3 4 10.00
5 6 7 8

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

C. Level Three (30%)


In this level, the program should read a set of eggs' sizes, divide them into two
categories, and output the minimal variance and egg distributions in each
category. Category with lesser elements will be printed first and sizes(eggs)
are sorted in ascending order within the category. Categories with same sizes
will be sorted based on the List sorting rule of Python.

Input: A list of egg's sizes in integer, separated by a space, terminated by a


carriage return (linefeed) character.

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

Case Sample Input Sample Output


1 7 2 6 4 5 1 3 8 10.00
[1 2 3 4 ][5 6 7 8 ]

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 ]

D. Level Four (20%)


Peter found that the size difference of eggs is quite unpredictable, he is
considering to divide his eggs into more than 2 categories. He believes your
programming skill could help him again.
Input:
• An integer K and a list of integers representing the sizes of eggs.
• K refers to the number of categories Peter want to have. You may
assume that K is any positive number greater than zero, and less than
or equal to the total number of eggs.

Output: Same as Level Three but each distribution consists of K categories.


Case Sample Input Sample Output
1 3 6.00
1 2 3 4 5 6 7 8 9 [1 2 3 ][4 5 6 ][7 8 9 ]

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.

Programming style and efficiency may be part of the assessment criteria.

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.

You might also like