COMPUTER SCIENCE TRIPOS

Similar documents
COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS Part II (General) DIPLOMA IN COMPUTER SCIENCE

CS126 Final Exam Review

COMPUTER SCIENCE TRIPOS Part II (General) DIPLOMA IN COMPUTER SCIENCE

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Assignment 1 (concept): Solutions

COMPUTER SCIENCE TRIPOS

Fun facts about recursion

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Midterm solutions. n f 3 (n) = 3

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

COMPUTER SCIENCE TRIPOS

Lecture 6: Sequential Sorting

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS

Framework for Design of Dynamic Programming Algorithms

Tribhuvan University Institute of Science and Technology 2065

CS 340 Spring 2019 Midterm Exam

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSci 4223 Principles of Programming Languages

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

COMPUTER SCIENCE TRIPOS Part II (General) DIPLOMA IN COMPUTER SCIENCE

Shell CSCE 314 TAMU. Functions continued

CSC-140 Assignment 5

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Programming Languages 3. Definition and Proof by Induction

Ryerson University Vers HAL6891A-09 School of Computer Science CPS109 Midterm Test Fall 09 page 1 of 7

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Analysis of Algorithms Part I: Analyzing a pseudo-code

ML Built-in Functions

CSE341 Spring 2016, Midterm Examination April 29, 2016

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Choice of C++ as Language

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Review for Midterm Exam

COMPUTER SCIENCE TRIPOS

An introduction introduction to functional functional programming programming using usin Haskell

The type checker will complain that the two branches have different types, one is string and the other is int

Programming in Haskell Aug-Nov 2015

Lab #10 Multi-dimensional Arrays

Algorithms and Data Structures

correlated to the Michigan High School Mathematics Content Expectations

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

Outline and Reading. Analysis of Algorithms 1

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

Introduction to Functional Programming: Lecture 7 2 Functions as innite data structures Ordinary ML data structures are always nite. We can, however,

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Lecture #23: Conversion and Type Inference

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

CSE341 Spring 2016, Midterm Examination April 29, 2016

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

SCHOOL OF COMPUTING, MATHEMATICAL AND INFORMATION SCIENCES Semester 2 Examinations 2008/2009. CI311 Specification and Refinement

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

CSE3322 Programming Languages and Implementation

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Recursion (Part 3) 1

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Lecture 19: Functions, Types and Data Structures in Haskell

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

To figure this out we need a more precise understanding of how ML works

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

OOP Exercise Sheet 2013/14

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

ANS:

CS321 Introduction To Numerical Methods

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

CPSC 320 Midterm 2 Thursday March 13th, 2014

Introduction to Algorithms and Complexity Theory

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Transcription:

CST.2010.1.1 COMPUTER SCIENCE TRIPOS Part IA NATURAL SCIENCES TRIPOS Part IA (Paper CS/1) POLITICS, PSYCHOLOGY, AND SOCIOLOGY TRIPOS Part I (Paper 7) Monday 31 May 2010 1.30 to 4.30 COMPUTER SCIENCE Paper 1 Answer one question from each of Sections A, B and C, and two questions from Section D. Submit the answers in five separate bundles, each with its own cover sheet. On each cover sheet, write the numbers of all attempted questions, and circle the number of the question attached. You may not start to read the questions printed on the subsequent pages of this question paper until instructed that you may do so by the Invigilator STATIONERY REQUIREMENTS Script paper Blue cover sheets Tags SPECIAL REQUIREMENTS Approved calculator permitted

CST.2010.1.2 SECTION A 1 Foundations of Computer Science (a) Give an ML datatype declaration suitable for representing lazy lists, possibly of infinite length. [2 marks] Code the ML function interleave, which takes two lazy lists and generates a lazy list containing each of their elements. [2 marks] Code an ML function that applies a given function to every element of a lazy list, returning a lazy list of the results (analogously to the function map). (d) Code the ML function iterates which, given a function f and some value x, generates a lazy list containing all the values of the form f n (x) (that is, f( f(x) ) with n applications of f) for n 0. (e) Code the ML function iterates2 which, given functions f and g and values x and y, generates a lazy list containing all the values of the form (f m (x), g n (y)) for m, n 0. [10 marks] All ML code must be explained clearly and should be free of needless complexity. 2

CST.2010.1.3 2 Foundations of Computer Science (a) Write brief notes on the function defined below: fun foldl f (e, []) = e foldl f (e, x::xs) = foldl f (f(e,x), xs); Illustrate your answer by describing the computations performed by the following two functions: fun f x = foldl (foldl op* ) (1,x); fun g p zs = foldl (fn ((x,y), z) => if p z then (z::x,y) else (x,z::y)) (([],[]), zs); Selection sort is a sorting algorithm that works by repeatedly identifying and setting aside the smallest (or largest) item to be sorted. Implement selection sort in ML and describe the efficiency of your solution using O-notation. Code an ML function to generate a multiplication table in the form of a list of lists of integers. For example, given the argument 3 it should return [[1, 2, 3], [2, 4, 6], [3, 6, 9]]. [6 marks] (d) Modify your solution to part in order to generate a three-dimensional table containing values x ijk computed by calling a supplied 3-argument curried function f. For example, given the argument 2 it should return [[[x 111,x 112 ], [x 121,x 122 ]], [[x 211,x 212 ], [x 221,x 222 ]]]. [6 marks] All ML code must be explained clearly and should be free of needless complexity. SECTION B 3 Discrete Mathematics I Give structured proofs or counterexamples for the following. (a) ((A B) (C B)) (A C ) [10 marks] ( x.a(x) y.a(y) y = x) ( x. y.(a(y) y = x)) [10 marks] 3 (TURN OVER)

CST.2010.1.4 4 Discrete Mathematics I Let x, y, z range over individuals I and a, b range over societies S. Let M, F and T be atomic predicates as follows: M (x, a) F (a) T (x, y, a) x is a member of society a society a involves fighting x talks to y about a (a) Formalise each of the following English statements and translate each of the following formulae into idiomatic English (natural English sentences). (i) x, y, a.t (x, y, a) T (y, x, a) (ii) Nobody talks to themselves about anything. (iii) There s at most one society involving fighting. (iv) All societies have at least two members. (v) a.( x, y.(m (x, a) M (y, a) x y)) x, y, b.m (x, a) M (y, a) x y T (x, y, b) F (vi) x, y, a.t (x, y, a) M (x, a) [12 marks] Is it possible to satisfy (i) (vi) simultaneously? Either give a concrete definition of two sets I and S and relations M, F, and T for which (i) (vi) are all true or prove that you can derive a contradiction from (i) (vi). Here are several attempts to formalise Somebody talks about everything. Explain what they actually mean, discussing whether or not each is a reasonable formalisation. (i) x. a. y.t (x, y, a) (ii) x. y. a.t (x, y, a) (iii) x. a. y.t (x, y, a) (iv) y. a. x.t (x, y, a) 4

CST.2010.1.5 SECTION C 5 Algorithms I (a) Describe the basic principle of the mergesort algorithm. Illustrate your answer by showing the steps involved in sorting the array { 9, 3, 6, 2, 4, 1, 5 }. [6 marks] Insertion sort can be considered as a mergesort where each step divides an array of size n into two arrays: one of size 1 (the element to be inserted) and one of size (n 1) for array length n. By solving an appropriate recurrence relation, show that this recursive version of insertion sort has a time complexity of O(n 2 ). Assume the time complexity for merging two arrays is O(n). A programmer is tasked with sorting both arrays and linked lists. For both data structures, he intends to use the mergesort algorithm. (i) Show that the time complexity of a linked list mergesort is O(n log n). Show also that the space complexity is O(1), taking care to demonstrate how this can be achieved. [6 marks] (ii) The programmer only knows how to merge two arrays in O(n) space and linked lists in O(1) space, so proposes converting the arrays to linked lists before applying the mergesort algorithm to save on space. Comment on this strategy. 5 (TURN OVER)

CST.2010.1.6 6 Algorithms I The product of two n n matrices, A and B, is a third n n matrix, Z, where n Z ij = A ik B kj k=1 (a) A programmer directly implements this formula when writing a function to multiply two matrices. Find the asymptotic time complexity of such an algorithm, taking care to justify your answer. An alternative strategy to compute Z is to divide A and B into four n 2 n 2 matrices. Computing Z then involves eight n 2 n 2 matrix multiplications followed by a series of matrix additions. This approach is then applied recursively. (i) Identify the algorithmic strategy in use. [1 mark] (ii) Show that the run time of this alternative strategy is given by the recurrence relation t n = Kt f(n) + O(g(n)) where t n is the time to compute the product of two n n matrices, K is a constant you should determine, and f(n) and g(n) are functions that you should identify. [6 marks] (iii) Solve the recurrence relation to find an asymptotic complexity for t n. An optimisation of the algorithm presented in part means that only seven matrix multiplications are needed rather than the eight previously suggested. State the new recurrence relation and solve it to show that this algorithm is O(n log 2 7 ). 6

CST.2010.1.7 SECTION D 7 Object-Oriented Programming with Java (a) Give three differences between an interface and an abstract class in Java. A novice programmer writes the following code in order to be able to completely clone an object of type Car. public class Tyre { private int treadremaining; } public void SetTread(int t) { treadremaining=t; } public int GetTread() { return treadremaining; } public class Car extends Vehicle implements Cloneable { private Tyre tyres[] = new Tyre[4]; } (i) public Car() { for (int i=0; i<4; i++) tyres[i] = new Tyre(); } public Object clone() throws CloneNotSupportedException { Car c = new Car(); c.tyres = this.tyres; return c; } Explain what it means for the treadremaining field to be private. Explain why it is good programming practice for such fields to be private. (ii) Identify the type of interface that Cloneable is. What is the defining characteristic of such interfaces? [2 marks] (iii) Identify and explain two reasons why this code may not function as intended. (iv) Rewrite the code to address the problems you have identified and allow Car objects to be fully cloned. [8 marks] 7 (TURN OVER)

CST.2010.1.8 8 Software Design You are required to present an outline design of a system that will be used by doctors at a medical practice to keep basic patient records. The system should record each consultation between a doctor and patient, any illness diagnosed by the doctor, and any drugs prescribed to the patient. At regular intervals, an auditor will use the system to check whether the same drug is being prescribed repeatedly to a particular patient. (a) Sketch a UML use case diagram for the above functionality. Sketch a UML class diagram for a system architecture to support this functionality. [6 marks] Provide outline Java class and method declarations for two of the classes that were included in part. (d) Sketch a UML interaction diagram showing the operation of the audit function. (e) Describe two variable roles that would be required when implementing the audit function in Java. 8

CST.2010.1.9 9 Floating-Point Computation (a) Describe the 64-bit ( double ) IEEE floating-point format, including special values. Explain the following terms: (i) absolute error; (ii) relative error; (iii) rounding error; (iv) truncation error; (v) machine epsilon. Outline how the implementation of the IEEE basic operations (+,,, /) are defined and their error properties. (d) The Taylor series for cosine converges for all values of x: cos x = 1 x2 2! + x4 4! Discuss issues in implementing a general-purpose library function that returns the value of cosine where the argument and result is a floating-point value. END OF PAPER 9