Beruflich Dokumente
Kultur Dokumente
INSTRUCTIONS TO CANDIDATES
1. The examination paper contains FIVE (5) questions and comprises TWENTY (20) pages.
2. Weightage of questions is given in square brackets. The maximum attainable score is 100.
3. This is a CLOSED book examination, but you are allowed to bring TWO double-sided
A4 sheets of notes for this exam.
4. Write all your answers in the space provided in this booklet.
5. Please write your matriculation number below.
MATRICULATION NUMBER:
F. [3 marks] In the remainder of this question, we assume that all lists are ascending lists, which
are non-empty lists of numbers whose elements are sorted in ascending order. For example, the
following lists are ascending lists:
list (44);
list (35 , 56 , 56 , 79);
list ( -20 , -10 , 0, 10 , 20);
However, the following lists are not ascending lists:
[];
list (56 , 35 , 79);
Define a function smallest_of_ascending_list that returns the smallest element of the given
ascending list. Example:
smallest_of_ascending_list ( list (35 , 56 , 56 , 79)); // returns 35
H. [4 marks] The median of an ascending list with an odd number of elements is the middle
value. The median of an ascending list with an even number of elements is the average (mean)
of the two middle values.
For example, the median of list(7) is 7, the median of list(20,50,60) is 50, and the
median of list(3, 17, 40, 50, 84, 99) is 45.
Define a function median that returns the median of the given ascending list. Examples:
median ( list (20 , 50 , 60)); // returns 50
median ( list (3 , 17 , 40 , 50 , 84 , 99)); // returns 45
5 CS1101S Final Examination — 27 November 2012
B. [4 marks] Write a function digit_sum that returns the sum of the digits of a given big
integer as an ordinary JavaScript number. Examples:
digit_sum ( make_big_int (" -" , list (1 ,2 ,3))); // returns number 6
digit_sum ( make_big_int ("+" , list (9 ,8))); // returns number 17
C. [2 marks] A number is divisible by 3 if and only if the sum of its digits is divisible by 3.
Using the function digit_sum above, write a function divisible_by_three that returns true
if the given big integer is divisible by 3 and false otherwise. Examples:
divisible_by_three ( make_big_int (" -" , list (1 ,2 ,3))); // returns true
divisible_by_three ( make_big_int ("+" , list (9 ,8))); // returns false
7 CS1101S Final Examination — 27 November 2012
E. [5 marks] In this question, we are given two big integers x and y and need to compute the
sum of their absolute values: |x| + |y|.
You may want to follow the notion of “long addition” from school, proceeding from right to
left, as depicted below.
Help provided: You can assume the functions align and add_three_digits as given below.
function align (xs , ys ) {
var lxs = length ( xs );
var lys = length ( ys );
var zeros = build_list ( Math . abs ( lxs - lys ),
function(x) { return 0; });
var newxs = ( lxs < lys ) ? append ( zeros , xs ) : xs ;
var newys = ( lxs > lys ) ? append ( zeros , ys ) : ys ;
function zip_them (xs , ys ) {
if ( is_empty_list ( xs ) || is_empty_list ( ys )) {
return [];
} else {
return pair ( pair ( head ( xs ), head ( ys )) ,
zip_them ( tail ( xs ), tail ( ys )));
}
}
return zip_them ( newxs , newys );
}
B. [3 marks] Use Θ notation to characterize the runtime of your solution for is_sorted with
respect to the size n of the given list.
C. [5 marks] Using the functions permutations and is_sorted described above, implement
a function sort that sorts a given list using the “generate-and-test” method. Examples:
sort ( list (2 ,1 ,3)); // returns same as: list(1,2,3)
sort ( list (17 ,35 ,17 ,4)); // returns same as: list(4,17,17,35)
12 CS1101S Final Examination — 27 November 2012
D. [3 marks] Assume that generating all permutations of a given list s has an order of growth
of Θ(n!) where n is the size of s. Use Θ notation to characterize the runtime of your sorting
function with respect to n.
B. [4 marks] Write a function has_token that takes a token ring r as argument and returns
true if and only if one element of r has the token. Make sure that your function terminates for
every token ring. Examples (using token_ring_1 and token_ring_2 from A):
has_token ( token_ring_1 ); // returns true
has_token ( token_ring_2 ); // returns false
C. [4 marks] Assume that you have token ring where one element has the token. Write a
function move_token that moves the token ahead by one position. Examples:
var token_ring_3 = list ( false , false , false );
set_tail ( tail ( tail ( token_ring_3 )) , token_ring_3 );
set_head ( token_ring_3 , true ); // now element 0 has the token
move_token ( token_ring_3 ); // now element 1 has the token
move_token ( token_ring_3 ); // now element 2 has the token
move_token ( token_ring_3 ); // now element 0 has the token
Your function move_token only needs to terminate, if the given token ring has the token.
15 CS1101S Final Examination — 27 November 2012
D. [4 marks] Write a function add_element that adds an element without token to a given
token ring. You are free to choose the position of the new element in the token ring. Examples:
var token_ring_4 = list ( true , false , false );
set_tail ( tail ( tail ( token_ring_4 )) , token_ring_4 );
// now token_ring_4 is a token ring with three elements
add_element ( token_ring_4 );
// now token_ring_4 is a token ring with four elements
E. [4 marks] Write a function ring_to_stream that returns an infinite stream of false and
true for a given token ring. Examples:
var token_ring_5 = list ( true , false , false );
set_tail ( tail ( tail ( token_ring_5 )) , token_ring_5 );
var token_stream_5 = ring_to_stream ( token_ring_5 );
// token_stream_5 is the stream:
// true, false, false, true, false, false, true, false, false,...
16 CS1101S Final Examination — 27 November 2012
B. [7 marks] Write a function all_subsequences that generates all subsequences of a given list.
Example:
all_subsequences ( list (1 ,2 ,3));
// returns the following subsequences:
// list( [], list(1), list(2), list(3),
// list(1,2), list(1,3), list(2,3),
// list(1,2,3)
// )
Note that the order in which the subsequences are listed does not matter.
18 CS1101S Final Examination — 27 November 2012
C. [7 marks] Consider two given lists s and t. The task is to find the length of the longest
sequence that is a subsequence of both s and t. More concretely, we are looking for a function
longest_subsequence_length(s,t)
that returns the length of the longest list that is a subsequence of both s and t. Example:
longest_subsequence_length ( list (1 ,2 ,6 ,1 ,3 ,1 ,4 ,1 ,2 ,6 ,1) ,
list (7 ,1 ,2 ,2 ,1 ,4 ,1 ,2 ,2 ,1 ,4 ,5 ,5));
// returns 7 because a longest subsequence is
// list(1,2,1,4,1,2,1)
One effective way of solving the task is to use dynamic programming. Describe how solutions to
smaller “longest subsequence” tasks can be used for solving larger “longest subsequence” tasks.
19 CS1101S Final Examination — 27 November 2012
Write a JavaScript function (or a sketch of it) longest_subsequence_length that makes use
of dynamic programming.
Hint: You do not need the functions in Part A and B for your solution.
— E N D O F P A P E R —
20 CS1101S Final Examination — 27 November 2012
Scratch Paper
– H A P P Y H O L I D A Y S ! –