Sie sind auf Seite 1von 20

1 CS1101S Final Examination — 27 November 2012

NATIONAL UNIVERSITY OF SINGAPORE


SCHOOL OF COMPUTING
EXAMINATION FOR
Semester 1, 2012/2013

CS1101S - PROGRAMMING METHODOLOGY (SCHEME)

27 November 2012 Time Allowed: 2 Hours

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:

(this portion is for the examiner’s use only)

Question Marks Remark


Q1
Q2
Q3
Q4
Q5
Total
2 CS1101S Final Examination — 27 November 2012

Question 1: Ascending Stars [20 marks]


A. [2 marks] Give the result of executing the following statement in JavaScript.
head ( list (1 ,2 ,3 ,4 ,5));

B. [2 marks] Give the result of executing the following statement in JavaScript.


list_ref ( list (1 ,2 ,3 ,4 ,5) ,2);

C. [2 marks] Give the result of executing the following statement in JavaScript.


length ( list (1 ,2 ,3 ,4 ,5));

D. [2 marks] Give the result of executing the following statement in JavaScript.


var x = 42;
var y = 5;
x / y;

E. [2 marks] Give the result of executing the following statement in JavaScript.


var x = 42;
var y = 5;
x % y;
3 CS1101S Final Examination — 27 November 2012

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

G. [3 marks] Define a function largest_of_ascending_list that returns the largest element


of the given ascending list. Example:
largest_of_ascending_list ( list (35 , 56 , 56 , 79)); // returns 79
4 CS1101S Final Examination — 27 November 2012

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

Question 2: Big Integers [20 marks]


In the lectures, we have seen that JavaScript can represent only a finite number of integers with
full precision. In this question, we are designing a “big integer” data structure that provides full
precision for arbitrarily large integers. The idea is to represent a number using a sign (the string
"+" or the string "-") and a list of decimal digits, each represented as an ordinary JavaScript
number ranging from 0 to 9.

A. [4 marks] We would like to define functions make_big_int, get_sign and get_digits


that allow us to construct and access such big integers. Example:
var bi = make_big_int ("+" , list (2 ,3 ,4)); // represents 234
get_sign ( bi ); // returns "+"
get_digits ( bi ); // returns list(2,3,4)
Define the function make_big_int, using object-oriented programming or other techniques from
the lectures.

Define the corresponding function get_sign.

Define the corresponding function get_digits.


6 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

D. [5 marks] Write a function big_int_to_number that returns an ordinary JavaScript number


that is as close as possible to the given big integer. Examples:
big_int_to_number ( make_big_int (" -" , list (1 ,2 ,3))); // returns -123
big_int_to_number ( make_big_int ("+" , list (9 ,8))); // returns 98
You are allowed to return +/-Infinity if the given big integer has more than 308 digits.
8 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 );
}

align ( list (1 ,2 ,3) , list (7 ,8 ,9));


// returns: list(pair(1,7),pair(2,8),pair(3,9))
align ( list (1 ,2 ,3 ,4 ,5) , list (7 ,8 ,9));
// returns: list(pair(1,0),pair(2,0),pair(3,7),pair(4,8),pair(5,9))
align ( list (7 ,8 ,9) , list (1 ,2 ,3 ,4 ,5));
// returns: list(pair(0,1),pair(0,2),pair(7,3),pair(8,4),pair(9,5))
9 CS1101S Final Examination — 27 November 2012

// add three digits (c is the carry digit)


function add_three_digits (d1 ,d2 ,c) {
var sum = d1 + d2 + c;
return pair ( sum % 10 , Math . floor ( sum / 10));
}
add_three_digits (8 ,9 ,1); // returns: pair(8,1)
add_three_digits (8 ,9 ,0); // returns: pair(7,1)
add_three_digits (4 ,3 ,0); // returns: pair(7,0)
add_three_digits (4 ,3 ,1); // returns: pair(8,0)
With this, write your function add_big_int(bi1, bi2). Example:
add_big_int ( make_big_int (" -" , list (8 ,7 ,6)) ,
make_big_int ("+" , list (2 ,0 ,5)));
// results in same big int as: make_big_int("+",list(1,0,8,1));
10 CS1101S Final Examination — 27 November 2012

Question 3: Generate-and-test [20 marks]


A general problem solving technique is called “generate-and-test”. The idea is to generate all
possible solution candidates for the problem, and then test them whether they have the desired
properties. Sorting a list in ascending order using generate-and-test involves generating all per-
mutations of a given list and then test each of the generated candidates, if it is sorted in ascending
order. In this question, you will implement and discuss such a sorting algorithm.
You can assume that you have a function permutations that generates all permutations of a
given list:
function permutations ( xs ) { ... }
permutations ( list (3 ,1 ,2));
// returns list( list(3,1,2), list(1,3,2), list(1,2,3),
// list(3,2,1), list(2,3,1), list(2,1,3) )
A. [5 marks] The first task is to implement a test that decides whether a given list is sorted in
ascending order. Thus:
is_sorted ( list (3 ,2 ,1)) // returns false
is_sorted ( list (3 ,1 ,2)) // returns false
is_sorted ( list (4 ,5 ,5)) // returns true
is_sorted ( list (1 ,2 ,3)) // returns true
is_sorted ( list (1)) // returns true
is_sorted ([]) // returns true
Give your solution for is_sorted here:
11 CS1101S Final Examination — 27 November 2012

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.

E. [4 marks] Is this “generate-and-test” method for sorting an efficient technique? Compare


this technique with a technique for sorting that you have seen in the Practical Examination.
13 CS1101S Final Examination — 27 November 2012

Question 4: Token Rings [20 marks]


In computer networks and distributed algorithms, the concept of a token ring is widely used.
A token ring is a circular list-like data structure of at least two elements, where at most one
element contains a “token”.
We will represent the elements as pairs, and the token by true. Examples:
var token_ring_1 = list ( false , true );
set_tail ( tail ( token_ring_1 ), token_ring_1 );
// now token_ring_1 is a token ring of two elements
// and one of its elements has the token

var token_ring_2 = list ( false , false , false );


set_tail ( tail ( tail ( token_ring_2 )) , token_ring_2 );
// now token_ring_2 is a token ring of three elements
// and none of its elements has the token
A. [4 marks] Draw box-and-pointer diagrams for the token rings token_ring_1 and token_ring_2
given above.
14 CS1101S Final Examination — 27 November 2012

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

Question 5: Subsequences [20 marks]


A subsequence of a given list s is a copy of s where none, some or all elements are deleted.
The order of elements remains unchanged. Examples:
var s = list (1 ,2 ,1 ,5 ,8);
var sequence_1 = list (1 ,1 ,5);
var sequence_2 = list (2 ,1 ,1);
var sequence_3 = [];
var sequence_4 = list (1 ,2 ,1 ,5 ,8);
Here, sequence_1, sequence_3 and sequence_4 are subsequences of s, but sequence_2 is not
a subsequence of s.

A. [6 marks] Write a function is_subsequence(xs, ys) that returns true if xs is a sub-


sequence of ys, and false otherwise. Examples:
is_subsequence ( sequence_1 , s) // returns true
is_subsequence ( sequence_2 , s) // returns false
is_subsequence ( sequence_3 , s) // returns true
is_subsequence ( sequence_4 , s) // returns true
17 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 ! –

Das könnte Ihnen auch gefallen