Foundations of Computer Science
Christmas Revision

Start your revision by going through the lecture notes and the supervision problems.

This sheet breaks down past exam questions by topic. Try to answer around one from each topic. For the “lists” topic do as many as you can. Take note of the questions you had trouble with, so we can go through them during the revision class. As you gain more experience restrict yourself to examination conditions.

For the revision class, the only requirement is to attempt the 2014-2016 past papers.

Language features

Type inference

Functions

Pattern matching:

Exceptions

Data structures

Lists

Queues

Binary trees

Binary search trees

Data types

Sorting

Permutations

Exercise [Check if a list is a permutation of another]: Write an OCaml function which given two lists determines if one is a permutation of the other. (This is 2009p1q1(d))

Exercise [Generalised permutation]: 2009p1q1 (e)

Exercise [Generate all possible permutations]: 2006p1q5 (b)

Exercise [Lazy permutation generation]: Modify your code about to lazily generate all permutations of a list.

Exercise [Generate all possible derangements]: Modify your code from the previous exercise to generate all possible derangements. A derangement is a permutation such that none of its elements maps back to its original position.

Exercise [Find the next permutation]: (+) Write an OCaml function which given a permutation computes the lexicographically next permutation. For example, given \([2; 3; 1; 4]\) it returns \([2; 3; 4; 1]\).

Exercise [Generate all combinations]: Given \(n\) and \(k\), write an OCaml function to generate all combinations. (Can you make it generate the combinations lazily?)

Parentheses

Exercise [Check if parentheses are balanced]: You are given a list containing ( and ). Write a program in OCaml to check if a sequence of parentheses are balanced. A sequence of open and closed parentheses being balanced is defined recursively:

Exercise [Generate all balanced parentheses]: Write a program in OCaml to generate all balanced parentheses.

Exercise [Lazy generation]: Modify your code from the previous exercise to lazily generate all balanced parentheses.

Infinite structures

Infinite lists

Infinite Binary trees

Games

The lecture notes contain no mention of games, but these have appeared in past exam questions. If you want to gain a basic background, read pages 1 to 10 from Ferguson’s book.

Imperative programming

Reference types

Mutability

(Optional) BFS/DFS Exercise in Java

In this exercise, you will solve the Lights Out puzzle using BFS, DFS and iterative deepening DFS. You are provided with Board.java (Java class that represents the Lights out board) and DfsSolution.java (Java class that implements a DFS solution for Lights Out).

  1. Argue why it is never beneficial to toggle the same light more than once.
  2. Familiarise yourself with the DFS solution in DfsSolution.java. Explain how/why it works.
  3. Run the DFS solution on the 4x4 board and on the 5x5 board and time it. Do these solutions use the minimum number of moves?
  4. Code a BFS solution for the problem and compare the solutions obtained with those obtained by the DFS in terms of time, memory and moves needed.
  5. Modify the DfsSolution code to become an iterative deepening DFS. (You should only need to modify a few lines in the explore function) Again, compare the solutions obtained with the standard DFS and BFS. What happens if you increase the board size?
  6. (Optional) Read this tutorial and write an efficient non-brute-foce solution for the 5x5 case. How does it compare to your previous solutions?