Question
Note: All ML code must be explained clearly (INJAVAXX)and should be free of needless complexity. 2 CST.2016.1.3 2 Foundations of Computer Science Please help. (2c)
Note: All ML code must be explained clearly (INJAVAXX)and should be free of needless complexity. 2 CST.2016.1.3 2 Foundations of Computer Science Please help.
(2c)
(a) A prime number sieve is an algorithm for finding all prime numbers up to a given limit n. The algorithm maintains a list, which initially holds the integers from 2 to n. The following step is then repeated: remove the head of this list, which will be a prime number, and remove all its multiples from the list. Write code for the algorithm above as an ML function of type int -> int list. [4 marks] (b) Consider the problem of eliminating all duplicates from a list of strings. Write code for a function of type string list -> string list such that the output contains the same elements as the input, possibly reordered, but where every element occurs exactly once. The worst-case performance must be better than quadratic in the length of the list. [6 marks] (c) Consider the task of determining whether a given word (a string) can be expressed by joining together various chunks (non-empty strings). If the chunks are abra, cad and hal, then the word abracadabra can be expressed as abra|cad|abra. Note that if the available chunks are ab, bra, cad and abra, then the first two are no good for expressing abracadabra, and yet a solution can be found using cad and abra. The chunks can be used any number of times and in any order. Write code for a function that takes a list of chunks along with a word, and returns a list of chunks that yield the word when concatenated. For the examples above, the result should be ["abra", "cad", "abra"]. Exception Fail should be raised if no solution exists. [10 marks] Note: You are given a function delPrefix for removing an initial part of a string. For example, delPrefix ("abra", "abracadabra") returns "cadabra", but delPrefix ("bra", "abracadabra") raises exception Fail. All ML code must be explained clearly and should be free of needless complexity. Help thanks!!
Well-known utility functions may be assumed to be available. 3 (TURN OVER) CST.2016.1.4 SECTION B 3 Object-Oriented Programming Java generics allows an ArrayList object to be constrained to use a single specific type (e.g. ArrayList). However, some applications require the ability to store objects of multiple unrelated types. In this question the aim is to store Integer objects alongside LinkedList objects. (a) One solution is to use ArrayList, since all Java objects extend Object. Explain why this is bad practice. [2 marks] (b) Seeking to provide a solution that allows an arbitrary set of constrained types, a programmer writes an abstract ConstrainedArray base class. To use it, the class is extended and a specialised void add(...) method should be provided for each acceptable type. public abstract class ConstrainedArray { protected ArrayList mArray = new ArrayList(); public Object get(int idx) {return mArray.get(idx);} public int size() { return mArray.size(); } } (i) Show how to a class IntListArray that extends this base class and accepts only Integer or LinkedList objects. Where appropriate, objects should be copied on insertion. [4 marks] (ii) Describe a sequence of events that would allow external modification of an object stored within an IntListArray, despite correct copying on insertion. How could this be addressed in IntListArray? [3 marks] (iii) By adding protected void add(Object o) {mArray.add(o);} to the ConstrainedArray class, the mArray field can be made private. Show how this would affect your IntListArray class and discuss the advantages of the change from protected to private. [5 marks] (c) The solutions in parts (a) and (b) both involve a get() method returning an Object reference. (i) Explain why this is bad practice. [1 mark] (ii) Propose an alternative solution for a constrained array of Integer or LinkedList objects (only) that addresses this issue. [5 marks] 4 CST.2016.1.5 4 Object-Oriented Programming (a) Using example Java code, distinguish between overloading, overriding and shadowing when applied to Java methods.
(e) Give a brief and rudimentary clarification of the standards of how the language coming about because of part (b) may be utilized to make a sentence structure analyser taking a token stream as information (by means of calls to work lex()) and giving as result an theoretical language structure tree relating to part (d).
2 Hosts 50/2/2 S0/1/0 R1 R2 GOM S2 PC2 GO1 50/1/1 NW:10.10.100.0/24 50/170 Network P: 15 Hosts Network Q: 10 Hosts Network Z: 2 Hosts Network Y: 2 Hosts 50/110 50/1/1 R3 G0/1 Network R: 4 Hosts 53 PC3 Part 1: Design the VLSM Address Scheme Divide the 10.10.100.0/24 network based on the number of hosts per subnet and complete the Subnet Table. You will subnet the network address 10.10.100.0/24. The network has the following requirements: Network P (LAN) requires 15 Usable host IP addresses. (Use the third available subnet) Network Q (LAN) requires 10 Usable host IP addresses. (Use the next available subnet) Network R (LAN) requires 4 Usable host IP addresses. (Use the next available subnet) Network X (WAN) requires 2 Usable host IP addresses. (Use the next available subnet) Network Y (WAN) requires 2 Usable host IP addresses. (Use the next available subnet) Network Z (WAN) requires 2 Usable host IP addresses. (Use the next available subnet) Subnet Table: Subnet Description Network Address First Host Address Second Host Address Last Host Address Broadcast Address Subnet Mask Network P Network Network R Network X NA Network Y NA Network Z NA Part 2: Complete the Addressing Table Document the addressing scheme and complete the addressing table. a. Assign the Last host IP address from Network P subnet to G0/1 interface of Ri. b. Assign the First host IP address from Network P subnet to Sl. c. Assign the Second host IP address from Network P subnet to PCI. d. Assign the Last host IP address from Network Q subnet to GO/1 interface of R2. e. Assign the First host IP address from Network Q subnet to S2. f. Assign the Second host IP address from Network Q subnet to PC2. g. Assign the Last host IP address from Network R subnet to G0/1 interface of R3. h. Assign the First host IP address from Network R subnet to S3. i. Assign the Second host IP address from Network R subnet to PC3. j. Assign the First host IP address from Network X subnet to S0/1/0 interface of Ri. k. Assign the Last host IP address from Network X subnet to S0/1/1 interface of R2. 1. Assign the First host IP address from Network Y subnet to S0/1/0 interface of R2. m. Assign the Last host IP address from Network Y subnet to S0/1/1 interface of R3. n. Assign the First host IP address from Network Z subnet to S0/1/0 interface of R3. 0. Assign the Last host IP address from Network Z subnet to S0/1/1 interface of Ri. (iii) Objects: Electronic health records (EHRs) in a nationwide service. Policy: The owner (patient) may read from its own EHR. A qualified and employed doctor may read and write the EHR of a patient registered with him/her. (iv) Object: The solution to online coursework. Policy: The coursework setter has read and access. A candidate has no access until after the marks have been published. [8 marks each] 4 CST.2004.13.5 5 Computer Graphics and Image Processing (a) Explain why display devices appear to be able to reproduce (almost) all the colours of the spectrum using only red, green and blue light. [4 marks] (b) Describe an algorithm (other than thresholding) which will convert a greyscale image (8 bits per pixel) to a bi-level black and white image (1 bit per pixel), with the same number of pixels, while retaining as much detail as possible. [8 marks] (c) Explain what specular and diffuse reflection are in the real world. State and explain equations for calculating approximations to both in a computer. [8 marks] 5 [TURN OVER CST.2004.13.6 6 Compiler Construction (a) Explain the differences (illustrating each with a small program) between (i) static and dynamic binding (scoping); [4 marks] (ii) static and dynamic typing. [2 marks] (b) Java is sometimes said to be "dynamically typed" in that a variable whose type is (class) C can be assigned a value of (class) D provided that D extends C; conversely a variable of type D can be assigned a value of type C using a cast. By considering storage layouts, explain why the former assignment is always valid and the latter sometimes invalid. [4 marks] (c) A new programming language has the notion of "statically scoped exceptions" in which the program exception foo; void f() { try { void g() { raise foo; } try { g(); } except (foo) { C2 } } except (foo) { C1 } } would execute C1 rather than C2 as the former was in scope at the raise point. By analogy with statically scoped variables, or otherwise, explain how such exceptions might be implemented on a stack. [10 marks] 6 CST.2004.13.7 7 Artificial Intelligence In the following, N is a feedforward neural network architecture taking a vector x T = ( x1 x2 xn ) of n inputs. The complete collection of weights for the network is denoted w and the output produced by the network when applied to input x using weights w is denoted N(w, x). The number of outputs is arbitrary. We have a sequence s of m labelled training examples s = ((x1, l1),(x2, l2), . . . ,(xm, lm)) where the li denote vectors of desired outputs. Let E(w; (xi , li)) denote some measure of the error that N makes when applied to the ith labelled training example. Assuming that each node in the network computes a weighted summation of its inputs, followed by an activation function, such that the node j in the network computes a function g w (j) 0 + X k i=1 w (j) i input(i) ! of its k inputs, where g is some activation function, derive in full the backpropagation algorithm for calculating the gradient E w = E w1 E w2 E wW T for the ith labelled example, where w1, .
(f ) Summarize any issues connected with left-or right-cooperative administrators in the two methods (in executing the parser and in developing the apparatus) you illustrated to some degree (e).
(iii) Objects: Electronic health records (EHRs) in a nationwide service. Policy: The owner (patient) may read from its own EHR. A qualified and employed doctor may read and write the EHR of a patient registered with him/her. (iv) Object: The solution to online coursework. Policy: The coursework setter has read and access. A candidate has no access until after the marks have been published. [8 marks each] 4 CST.2004.13.5 5 Computer Graphics and Image Processing (a) Explain why display devices appear to be able to reproduce (almost) all the colours of the spectrum using only red, green and blue light. [4 marks] (b) Describe an algorithm (other than thresholding) which will convert a greyscale image (8 bits per pixel) to a bi-level black and white image (1 bit per pixel), with the same number of pixels, while retaining as much detail as possible. [8 marks] (c) Explain what specular and diffuse reflection are in the real world. State and explain equations for calculating approximations to both in a computer. [8 marks] 5 [TURN OVER CST.2004.13.7 8 Compiler Construction (a) Explain the differences (illustrating each with a small program) between (i) static and dynamic binding (scoping); [4 marks] (ii) static and dynamic typing. [2 marks] (b) Java is sometimes said to be "dynamically typed" in that a variable whose type is (class) C can be assigned a value of (class) D provided that D extends C; conversely a variable of type D can be assigned a value of type C using a cast. By considering storage layouts, explain why the former assignment is always valid and the latter sometimes invalid. [4 marks] (c) A new programming language has the notion of "statically scoped exceptions" in which the program exception foo; void f() { try { void g() { raise foo; } try { g(); } except (foo) { C2 } } except (foo) { C1 } } would execute C1 rather than C2 as the former was in scope at the raise point. By analogy with statically scoped variables, or otherwise, explain how such exceptions might be implemented on a stack. [10 marks] 6 CST.2005.13.8 8 Artificial Intelligence In the following, N is a feedforward neural network architecture taking a vector x T = ( x1 x2 xn ) of n inputs. The complete collection of weights for the network is denoted w and the output produced by the network when applied to input x using weights w is denoted N(w, x). The number of outputs is arbitrary. We have a sequence s of m labelled training examples s = ((x1, l1),(x2, l2), . . . ,(xm, lm)) where the li denote vectors of desired outputs. Let E(w; (xi , li)) denote some measure of the error that N makes when applied to the ith labelled training example. Assuming that each node in the network computes a weighted summation of its inputs, followed by an activation function, such that the node j in the network computes a function g w (j) 0 + X k i=1 w (j) i input(i) ! of its k inputs, where g is some activation function, derive in full the backpropagation algorithm for calculating the gradient E w = E w1 E w2 E wW T for the ith labelled example, where w1, . . . , wW denotes the complete collection of W weights in the network. [20 marks] 7 [TURN OVER CST.2004.13.8 8 Databases (a) Define the operators of the core relational algebra. [5 marks] (b) Let R be a relation with schema (A1, . . . , An, B1, . . . , Bm) and S be a relation with schema (B1, . . . , Bm). The quotient of R and S, written R S, is the set of tuples t over attributes (A1, . . . , An) such that for every tuple s in S, the tuple ts (i.e. the concatenation of tuples t and s) is a member of R. Define the quotient operator using the operators of the core relational algebra. [8 marks] (c) The core relational algebra can be extended with a duplicate elimination operator, and a grouping operator. (i) Define carefully these two operators. [3 marks] (ii) Assuming the grouping operator, show how the duplicate elimination operator is, in fact, unnecessary. [2 marks] (iii) Can the grouping operator be used to define the projection operator? Justify your answer. [2 marks] 8 CST.2004.13.9 9 Numerical Analysis II (a) A Riemann integral over [a, b] is defined by Z b a f(x) dx = limn 0 Xn i=1 (i i1)f(xi) . Explain the terms Riemann sum and mesh norm. [4 marks] (b) Consider the quadrature rule Qf = 3h 8 [f(a) + 3f(a + h) + 3f(a + 2h) + f(a + 3h)] 3f (4)()h 5 80 . If [a, b] = [1, 1] find 0, 1, . . . , 4 and hence show that this is a Riemann sum. [3 marks] (c) Suppose R is a rule that integrates constants exactly over [1, 1], and that f(x) is bounded and Riemann-integrable over [a, b]. Write down a formula for the composite rule (n R)f and prove that limn (n R)f = Z b a f(x) dx [6 marks] (d) What is the formula for (n Q)f over [a, b]? [4 marks] (e) Which polynomials are integrated exactly by Qf? Which monomials are integrated exactly by the product rule (Q Q)F when applied to a function of x and y? [3 marks] 9 [TURN OVER CST.2004.13.10 10 Introduction to Functional Programming (a) Define a polymorphic datatype 'a tree to represent binary trees. [1 mark] (b) A breadth-first traversal of a tree walks over all the nodes at each level before proceeding to the next level. For example the breadth-first traversal of the tree: 1 2 3 4 5 6 7 visits the nodes in the order 1, 2, 3, 4, 5, 6, 7. Define a function breadth: 'a tree -> 'a list such that breadth(t) returns the nodes of tree t in breadth-first order. [10 marks] (c) Define a polymorphic datatype 'a seq to represent lazy lists. [1 mark] (d) Define a polymorphic datatype 'a ltree to represent lazy binary trees. [3 marks] (e) Define a function inorder of type 'a ltree -> 'a seq that traverses a lazy tree in-order, returning the nodes in a lazy list. (You should define any auxiliary functions you may use.) [5 marks] 10 CST.2004.13.11 11 Natural Language Processing (a) Give brief definitions of the following terms: (i) referring expression; (ii) cataphora; (iii) pleonastic pronoun. [6 marks] (b) Describe the Lappin and Leass algorithm for pronoun resolution, illustrating its operation on the text below. Exact weights for salience factors are not required. Owners love the new hybrid cars. They all say that they have much better fuel economy than conventional vehicles. And it seems that the performance of hybrid cars matches all expectations. [14 marks] 11 CST.2004.13.12 12 Complexity Theory (a) Define a one-way function. [4 marks] (b) Explain why the existence of one-way functions would imply that P6=NP. [7 marks] (c) Recall that Reach is the problem of deciding, given a graph G a source vertex s and a target vertex t, whether G contains a path from s to t; and Sat is the problem of deciding whether a given Boolean formula is satisfiable. For each of the following statements, state whether it is true or false and justify your answer. (i) If Reach is NP-complete then P=NP. [3 marks] (ii) If Reach is NP-complete then NP6=PSPACE. [3 marks] (iii) If Sat is PSPACE-complete then NP=PSPACE. (f ) Summarize any issues connected with left-or right-cooperative administrators in the two methods (in executing the parser and in developing the apparatus) you illustrated to some degree (e).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started