Optional 1 (Year 2 Sem 2)

Optional1 is the first optional in semester 2 of year 2, and the first optional in the total of year 2. It is an extension of A&A, and is considered to be moderately difficult.

Description
No description was provided for this optional.

Unlocked after completion
None

To unlock

 * Complete Lab 1 (Year 2 Sem 2)

Question
Question 1: Candies

In front of Benjamin, there are 10 candies, arranged in a row. A certain amount of satisfaction is obtained for eating each of the candies. Benjamin must eat the candies in the order they appear and once he stops eating he cannot eat any more. His goal is to obtain an amount of satisfaction as close as possible to 100.

In case there exist two such amounts which equally close 100 (eg, 110 and 90), he will pick the greater one. (in this case, 110).

Help Benjamin find how many amount he will obtain.

Implementation Sample
 * implement the function candies( input )
 * input is the list with 10 positive integer value, each of them is less than or equal to 100, denoting the amount of satisfaction for eating each candy, in the order that Benjamin can eat them.
 * this function returns the required number of amount of satisfaction.
 * candies( [10,20,30,40,50,60,70,80,90,100]) returns 100

Solution
Eat candies while the next candy would bring you closer to 100 satisfaction.

If both are equally close, the question requires you to choose the larger amount.

Question
Question 2: Sequence

Alan is playing with some numbers. Firstly, he writes down a sequence of integer A. Then he creates another sequence of integer B, by replacing every number from A with the average value of all integers up to and including the current one.

For example, if A is 3,1,5,11, then B should be 3/1, (3+1)/2, (3+1+5)/3, (3+1+5+11)/4, which are 3, 2, 3, 5.

You are given the sequence B, determine the sequence A.

Implementation

·       Implement a function Q1(lst), where the input lst is a list of integers, representing the sequence B as described above.

·       This function returns a list, representing the sequence A as described above

Sample

·       Q1( [3,2,3,5] ) = [3,1,5,11]

·       Q1( [3] ) = [3]

Solution
The first number in B is guaranteed to be the first number in A, as the average of a list with 1 element is that element. (There is no empty list corner case.)

Continue adding on to A from there.