Sunteți pe pagina 1din 10

# Midterm Grand Tutorial General CS I (320101)

## October 29, 2010

Mathematical Foundations
0pt

Problem 1.1 (Sums of numbers) Prove that an integer n 1 can be represented in n ways as the sum of one or more positive integers, such that the dierence between each two of these integers is at most 1. Use induction for your proof. For example: the number 2 has 2 representations: 1 + 1 and 2. Solution: Proof : We will prove that each non-zero natural number n can be represented
in n ways as such a sum P.1 For the induction we have to consider the following cases: P.1.1 n = 1: P.1.1.1 1 = 1 is one representation, i.e. the statement holds. P.1.2 n = 2: P.1.2.1 It is not required to do a second base-case. P.1.2.2 From the example it is obvious that the statement holds. P.1.3 n > 1: P.1.3.1 Now, assume that the assertion is true for a certain n = k, i.e. that the number k can be represented in k ways as the sum of nonzero natural numbers with dierence at most 1. P.1.3.2 Next, we prove this for n = k + 1. It is obvious that in each of the k sums representing the number k, there are at most two dierent summands. Therefore, if we add 1 to one of the smaller summands in each sum, we will still get k dierent sums, all equal to k + 1, and for which the property that the summands are with dierence at most 1 still holds. In this way, however, there is no sum consisting only of 1s. Thus we add it, i.e. a sum

of k + 1 1s. So there are k + 1 dierent sums with the required property representing the number k + 1. Thus we have proven by induction that the statement holds.

Problem 1.2 (Relation Properties) Give a formal denition in math talk for each of the following relation properties: 1. reexivity 2. symmetry 3. transitivity 4. antisymmetry Then check whether the following relations over the set A := {a, b, c, d, e} have the properties. If not, give a counter-example: 1. R1 A A, 2. R2 A A, 3. R3 A A, Solution:
1. a A. a, a R 2. a, b A. a, b R b, a R 3. a, b, c A.( a, b R b, c R) a, c R 4. a, b A.( a, b R b, a R) a = b 1. R1 is symmetric; not reexive (since d, d is not in it); not transitive ( d, a and a, d imply that d, d , should be there, but it is not; therefore it is not antisymmetri eiter. 2. R2 is reexive, transitive and antisymmetric; it is not symmetric(e.g. a, b is there, but b, a is not) 3. R3 is transitive; it is not symmetric, since c, c is not in it; it is not symmetric since, e.g. it contains c, d , but not d, c ; it is not antisymmetric since it contains both a, c and c, a

0pt

R1 := { a, a , a, d , d, a , b, b , c, c , b, c , c, b }

R2 := { a, a , b, b , c, c , d, d , e, e , e, d , a, b , e, c } R3 := { a, c , c, d , d, a , a, d , c, a , d, d , b, b }

0pt Problem 1.3 (Truth values) 0 0 Consider the following ADT for sets of unary natural numbers: A := S , , where S := {N, S, B} and := {[o : N], [s : N N], [ : S], [i : N S], [u : S S S], [T : B], [F : B]}. N is the sort of unary natural numbers S is the sort of sets of unary natural numbers B is the sort of boolean values Your task is to dene the abstract procedures corresponding to: the logical operator and the logical operator or the equality of unary natural numbers the element of operator (concerning sers of unary natural numbers) Solution:
the logical operator and: ::B B B ; {(T, T ) the logical operator or: ::B B B ; {(T, T ) T , (T, F ) T , (F, T ) T , (F, F ) F} T , (T, F ) F , (F, T ) F , (F, F ) F}

the equality of unary natural numbers: =::N N ; {=(o, o) T , =(o, s(x)) F , =(s(x), o) F , =(s(x), s(y)) =(x, y)}

the element of operator (concerning sers of unary natural numbers): ::N S B ; {(x, ) F , (x, i(x)) =(x, y), (x, u(a, b)) ((x, a), (x, b))}

Problem 1.4

(Star systems)

0pt

1. Design an abstract data type for a star system. A star system consists of the following objects: Moon, M a simple space object with a name Planet, P a simple space object with a name Star, S a simple space object with a name Planetary System, PS an anonymous grouping of one planet and 0 or more moons that orbit around the planet (also called planetary satellites) Star System, SS an anonymous grouping of one or more stars and 0 or more planetary systems that orbit around the star(s) (also called star satellites) Use this ADT for names/words to construct simple space objects from it:

{L}, {[a : L], [b : L], . . ., [z : L], [A : L], . . ., [Z : L], [ : L], [concat : L L L]} 2. Now represent the following two diagrams using your ADT:

Solar System

Tatoo System 5

Take as granted terms representing names of objects, e.g. the names Sun, Mercury, Tatoo I, etc. Solution:
1. [a : L], [b : L], . . . , [z : L], [A : L], . . . , [Z : L], [ : L], [concat : L L L] [moon : L M] [planet : L P] {L, M, P, S, PS, SS}, [pl : P PS], [plsat : PS M PS] [st : S SS], [mults t : SS S SS], [stsat : SS PS SS] 2. stsat(stsat(stsat(st(star(Sun)), pl(planet(Mercury)) ), pl(planet(Venus)) ), plsat(pl(planet(Earth)), moon(Moon)) ) stsat( stsat( stsat( mult_st( st(star(TatooI)),star(TatooII) ), plsat( plsat( plsat( pl(planet(Tatooine)), moon(Ghomrassen) ), moon(Guermessa) ), moon(Chenin) ) ), pl(planet(Ohann)) ), pl(planet(Adriana)) )

Programming in Standard ML
0pt

Problem 2.1 (Crates at sea) A certain ship is at sea on a voyage from Bremen to New York. There is a crate on her deck. However the crate has not been attached/xed properly and when in a slight storm the ship goes over a wave, the crate can move one meter to the right or to the left. When it reaches the edge of the deck it is stopped by the rails. Assume that the crate is originally in the middle of the deck and that it is 1 meter wide. Write an SML function that given the half the decks width (so the distance from the original position of the crate to the edge of the deck) and a string describing the crates movements, determines how many times the crate will reach the edges of the deck. The string consists of the words right, left for the respective movements. (You can ignore any other characters in between the words). The function will have the following signature:
val count = fn : string int > int

Example:
count(right left left left left left a right right right right a right right right right right left right move left,4); val it = 3 : int

Solution:
fun movements([],width, possition, times) = ([],width, possition, times) | movements((#l)::(#e)::(#f)::(#t)::l, width, possition, times) = if (possition 1) = 0width then movements(l,width, possition1,times+1) else if possition = 0width then movements(l,width, possition,times) else movements(l,width, possition1,times) | movements((#r)::(#i)::(#g)::(#h)::(#t)::l, width, possition,times) = if (possition + 1) = width then movements(l,width, possition+1,times+1) else if possition = width then movements(l,width, possition,times) else movements(l,width, possition+1,times) | movements(a::l, width, possition, times) = movements(l, width, possition,times); fun count(l,width) = let val (m,w,p,t) = movements(explode(l),width,0,0) in t end;

0pt Problem 2.2 (Partitions) A partition of a set M is a set of nonempty subsets of M , such that every element in M is in exactly one of these subsets. For example, one of the partitions of {1, 2, 3, 4} would be {{1, 2}, {3}, {4}}. Write an SML function which takes a list of integers as argument, and returns all of its partitions:
val partition = fn : int list > int list list;

Example:
partition [1,2,3]; val it = [[[1,2,3]],[[1],[2,3]],[[1,2],[3]],[[2],[1,3]],[[1],[2],[3]]]

Solution:
Control.Print.printLength := 1000; fun append x ls = map (fn u => x :: u) ls fun insertInNth x ll pos = let val l = List.nth(ll, pos 1) val l1 = List.take(ll, pos 1) val l2 = List.drop (ll, pos) in l1 @ [x :: l] @ l2 end fun upto 1 = [1] | upto n = upto (n 1) @ [n] fun insertInAll x ls = map (insertInNth x ls) (upto (length ls)) fun partitionInK [] k = [] | partitionInK ls 1 = [[ls]] | partitionInK (h :: t) k = let val nkm1 = partitionInK t (k 1) val nk = partitionInK t k val single = append [h] nkm1 val comb = List.concat (map (insertInAll h) nk) in single @ comb end fun partition ls = List.concat (map (partitionInK ls) (upto (length ls)))

0pt Problem 2.3 (Chocolates and calories) Now that you nally opened your chocolates factory in Bremen, you would like to nally write an SML function that can help you compute the amount of calories of your box of chocolates. You are given the following SML datatypes that model an ingredient, a chocolate and the box of chocolates:
datatype ingredient = topping of real | proteins of realint | carbohydrates of real; datatype chocolate = choco of real | ing of ingredient list; datatype chocobox = box of chocolate list;

You know that the topping adds a constant of 50.0 calories, no matter its quantity. There are two types of proteins: for proteins of type 1, only half of the quantity is converted to calories, while for proteins of type 2, you get twice as much calories as their quantity. You know that every unit of carbohydrates is converted to one calory. In the datatype, you get the quantity of each ingredient (and also the type of the protein). For some chocolates, you directly know the number of calories (the composition is secret), while for the others you are given the list of ingredients and you have to compute the number of calories yourself. Given this information, you are required to write an SML function countCalories that, given a chocobox, will return the number of calories the box contains. The function will have the following signature:
val countCalories = fn : chocobox > real

Example:
countCalories( box([choco(120.0), ing([topping(1.5), proteins(20.0, 1), carbohydrates(37.0), proteins(12.0, 2) ]) ]) ); val it = 241.0 : real

Solution:

datatype ingredient = topping of real | proteins of realint | carbohydrates of real; datatype chocolate = choco of real | ing of ingredient list; datatype chocobox = box of chocolate list; fun countCalories( box(theList) ) = let fun calories (choco(X)) = X | calories (ing(P)) = let fun f [] = 0.0 | f (topping(X)::l) = 50.0 + f(l) | f (proteins(X,Y)::l) = (X (if Y=1 then 0.5 else 2.0)) + f(l) | f (carbohydrates(X)::l) = X + f(l) in f(P) end in foldl (fn (X,Y) => calories(X) + Y) 0.0 theList end;

10