Midterm Grand Tutorial General CS I (320101

October 29, 2010


Mathematical Foundations

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 difference 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 difference 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 different summands. Therefore, if we add 1 to one of the smaller summands in each sum, we will still get k different sums, all equal to k + 1, and for which the property that the summands are with difference at most 1 still holds. In this way, however, there is no sum consisting only of 1’s. Thus we add it, i.e. a sum


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

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

[T : B]. ∨(T.3 (Truth values) 0 0 Consider the following ADT for sets of unary natural numbers: A := S . {∧(T. b))} 4 . Σ . [F : B]}. y)} • the “element of” operator (concerning sers of unary natural numbers): ∈::N × S → B . F ) F} • the equality of unary natural numbers: =::N → N . where S := {N. b)) ∨(∈(x. [s : N → N]. F ) F} T . ∈(x. [Φ : S]. ∨(F. {=(o. =(o. o) T . y). ∧(F. a). F ) T . ∈(x. S. s(x)) F .0pt Problem 1. ∨(F. ∈(x. F ) F . s(y)) =(x. =(s(x). • 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 define 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. i(x)) =(x. T ) T . T ) T . B} and Σ := {[o : N]. o) F . u(a. =(s(x). {∈(x. {∨(T. [i : N → S]. T ) F . Φ) F . [u : S × S → S]. ∧(F. T ) • the logical operator “or”: ∨::B × B → B .

. [b : L]. . A star system consists of the following objects: • Moon. Design an abstract data type for a star system. . . [concat : L × L → L]} 2. [ : L].4 (Star systems) 0pt 1.. . [z : L]. . S – a simple space object with a name • Planetary System. M – a simple space object with a name • Planet. {[a : L]. [A : L]. P – a simple space object with a name • Star. [Z : L]. 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}. Now represent the following two diagrams using your ADT: Solar System Tatoo System 5 ..Problem 1.

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

width. possition. possition.width. When it reaches the edge of the deck it is stopped by the rails.times) | movements(a::l. possition. possition. val it = 3 : int Solution: fun movements([]. times) = if (possition − 1) = 0−width then movements(l. ”left” for the respective movements.times+1) else if possition = width then movements(l. 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. 7 .times) | movements((#”r”)::(#”i”)::(#”g”)::(#”h”)::(#”t”)::l. 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”.0) in t end. possition+1. (You can ignore any other characters in between the words). width.times) else movements(l. times) = movements(l.1 (Crates at sea) A certain ship is at sea on a voyage from Bremen to New York. possition+1. times) = ([].width. width.2 Programming in Standard ML 0pt Problem 2.w.width.times) = if (possition + 1) = width then movements(l.times). The string consists of the words ”right”.width. fun count(l. possition−1. possition.p.t) = movements(explode(l).width) = let val (m. Assume that the crate is originally in the middle of the deck and that it is 1 meter wide. possition.width. width.width. times) | movements((#”l”)::(#”e”)::(#”f”)::(#”t”)::l.4). determines how many times the crate will reach the edges of the deck.width. There is a crate on her deck.0. possition.times) else movements(l. However the crate has not been attached/fixed properly and when in a slight storm the ship goes over a wave. width.times+1) else if possition = 0−width then movements(l. the crate can move one meter to the right or to the left.width. possition−1. possition.

[[1].3]].concat (map (partitionInK ls) (upto (length ls))) 8 . 3.[[1]. 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.0pt Problem 2. val it = [[[1.3]]. and returns all of its partitions: val partition = fn : int list −> int list list.[[2].[[1. {4}}.printLength := 1000.Print.[3]]] Solution: Control.[1. pos − 1) val l1 = List.2.2].2. one of the partitions of {1. pos − 1) val l2 = List. 2. For example. such that every element in M is in exactly one of these subsets.3]]. Write an SML function which takes a list of integers as argument. 4} would be {{1.[2. fun append x ls = map (fn u => x :: u) ls fun insertInNth x ll pos = let val l = List.2 (Partitions) A partition of a set M is a set of nonempty subsets of M .[2].take(ll. {3}. Example: − partition [1.3].drop (ll.[3]].concat (map (insertInAll h) nk) in single @ comb end fun partition ls = List.nth(ll. 2}.

proteins(20. you get the quantity of each ingredient (and also the type of the protein). ing([topping(1.0). no matter its quantity. will return the number of calories the box contains. You are given the following SML datatypes that model an ingredient. For some chocolates. There are two types of proteins: for proteins of type 1. In the datatype. You know that the topping adds a constant of 50. datatype chocobox = box of chocolate list. The function will have the following signature: val countCalories = fn : chocobox −> real Example: − countCalories( box([choco(120. only half of the quantity is converted to calories. you get twice as much calories as their quantity.0.5). while for proteins of type 2.0 : real Solution: 9 .0). carbohydrates(37. Given this information. while for the others you are given the list of ingredients and you have to compute the number of calories yourself. You know that every unit of carbohydrates is converted to one calory. you are required to write an SML function countCalories that. 1). proteins(12.0 calories. a chocolate and the box of chocolates: datatype ingredient = topping of real | proteins of real∗int | carbohydrates of real.0pt Problem 2. given a chocobox.3 (Chocolates and calories) Now that you finally opened your chocolates factory in Bremen. you would like to finally write an SML function that can help you compute the amount of calories of your box of chocolates. 2) ]) ]) ). you directly know the number of calories (the composition is secret). val it = 241. datatype chocolate = choco of real | ing of ingredient list.0.

datatype chocolate = choco of real | ing of ingredient list.0 | f (topping(X)::l) = 50.5 else 2. 10 . fun countCalories( box(theList) ) = let fun calories (choco(X)) = X | calories (ing(P)) = let fun f [] = 0. datatype chocobox = box of chocolate list.Y)::l) = (X ∗ (if Y=1 then 0.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.0 + f(l) | f (proteins(X.datatype ingredient = topping of real | proteins of real∗int | carbohydrates of real.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.