ML practice problems
List processing
Easy
- Write a function which, given a list of
int
s
will return a list of int * int
tuples containing each element
of the list twice.
For example, given the list [4,6,3,1]
, it will return the
list [(4,4), (6,6), (3,3), (1,1)]
.
Solution.
- Write a function which, given a list of
int
s and
another int
will determine whether or not the
list contains a multiple of the given int
.
For example, given [1, 12, 8, 15]
and 3
,
it will return true;
given [1, 12, 8, 15]
and 7
,
it will return false.
Solution.
- Write a function which, given a list of
int
s
will return a list of int
s like the one given
except that each number is replaced by the sum of all numbers after it.
For example, given the list [4,6,3,1]
, it will return the
list [14, 10, 4, 1]
.
Solution.
Medium
- Write a function which, given an
int
x will return a list containing the number x repeated
x times.
For example, given 0
, it will return []
;
given 4
, it will return [4, 4, 4, 4]
.
Solution.
-
Write a function
deleteNth
which takes a list and an
integer and returns a list like the one it receives except that
the nth element is deleted (counting from 1).
For example, deleteNth([2, 5, 4, 3, 6, 3], 3)
will return [2, 5, 3, 6, 3]
.
(You may assume that n is greater than or equal to zero.
If n is 0 or if it is greater than the length of the list, then return the
whole list.)
Solution.
Hard
-
Write an ML function
deleteEveryNth
which takes a list
and an integer and returns a list like the one it receives except th
at
every nth element is deleted.
For example, deleteEveryNth([2, 5, 4, 3, 6, 3], 3)
will return [2, 5, 3, 6]
.
(This time you may assume n is greater than zero.)
Solution.
- Write a function which, given a list
will return the two halves of the list in a tuple (if the list has an odd number
of elements, then one "half" will need to be one element longer than the other).
For example, given
[3, 12, 4, 6, 73, 34, 86]
,
it will return ([3, 12, 4, 6], [73, 34, 86])
Solution.
- Write a function which, given two lists of
int
s,
which you may assumed to be sorted from smallest to largest, returns
a list merged from the two given lists, with the numbers still in order.
For example, given [3, 5, 13, 18]
and [4, 8, 9, 10]
will return [3, 4, 5, 8, 9, 10, 13, 18]
Solution.
- Using your answers to the two previous questions, write
a function which, given a list of
int
s, will sort
the list by splitting the list in half, recursively sorting each half list,
and then merging the sorted results.
Solution.
Relations
Easy
- Write a function which, given the list representation of a relation
and an element returns the image of that element under the relation (as a list).
For example, given
[(1, 3), (3, 6), (2, 1), (1, 6), (5, 4), (1,9)]
and 1
, it will return [3, 6, 9]
(or the numbers could be in a different order).
Solution.
- Write a function which, given a list representation of a relation,
returns the predicate representation of that relation.
Solution.
Medium
- Write a function which, given a predicate representation of a relation
and a list of elements, tests whether or not the relation is reflexive
for those elements.
For example, given
fn (x,y) => (x + y) mod 3 = 0
(that is,
x and y are related if their sum is a multiple of 3)
and [3, 9, 12]
returns true, but given that
function and [3,9,12,11]
returns false.
Solution.
- Write a function which, given a list representation of a relation,
will determine whether or not the relation is symmetric.
Solution.
- Write a function which, given a predicate representation of a relation,
will return the inverse of that relation (as a predicate).
Solution.
Hard
- Suppose R is a relation from a set A to a set B
and S is a relation from B to a set C.
Write a function which, given the predicate representations of R and
S and also the entire set B as a list
will return the composition SoR as a predicate.
Solution.
First class functions
Easy
- Write a function which, given a list and a function,
will return a list of containing the results of the given function
applied to every element in the list.
For example, given
[1, 2, 3, 4]
and fn (x) => 2 * x
,
it will return [2,4,6,8]
Solution.
- Write a function which, given a list, will return a function which,
given an integer n will return the nth element in the given list.
For example, if your function were named
lister
,
then lister([1,2,3,4])(3)
would return 3
.
Solution.
Thomas VanDrunen
Last modified: Mon Dec 17 12:26:47 CST 2007