Download 09

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Elementary mathematics wikipedia , lookup

Collatz conjecture wikipedia , lookup

Sequence wikipedia , lookup

Series (mathematics) wikipedia , lookup

Addition wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
Introduction to Programming – I
1. Given a list of integers a1 , a2 , . . . an , the aim is to find a subsequence of minimum sum such
that subsequence contains at least one of every pair of adjacent positions. Your aim is to
write a haskell function best :: [Int] -> (Int,[Int]) which returns the best possible
sum and the indices of the positions in the subsequence giving this sum. If there are multiple subsequences giving the same answer, you must return the one whose list of indices is
lexicographically the least.
best [3,1,1,4] = (2,[1,2])
best [1,2,3,2] = (4,[0,2])
In the second example the sum 4 can be obtained by picking either positions 0 and 2 or
the positions 1 and 3, the former is to be returned since [0, 2] is smaller than [1, 3] in the
lexicographic order.
2. This is a game played with a sequence of tiles, each labelled with two numbers. You start at
the first tile in the sequence and choose one number from each tile that you stop at, according
to the following rules.
• At tile i, if you pick up the smaller number, you move on to the next tile, i+1, in the
sequence.
• At tile i, if you pick up the larger number, you skip the next tile and move to tile i+2,
in the sequence.
The game ends when your next move takes you beyond the end of the sequence. Your score
is the sum of all the numbers you have picked up. Your goal is to maximise your score.
For example, suppose you have a sequence of four tiles as follows:
Tile 1
1 2
Tile 2
1 3
Tile 3
1 −1
Tile 4
−2 −3
Then, the maximum score you can achieve is 3, by choosing the numbers that are circled.
Write a function best ::
a given list of tiles.
[(Int,Int)] -> Int that computes the best possible score for
3. Recall the problem of determining the minimum number of operations to mulitply out a sequence of K matrices. Write a function matmul :: [Int] -> (Int,String) that computes
a bracketing that yields the optimal answer.
For instance, if the given a sequence has 3 matrices with dimensions 10 × 30, 30 × 5 and 5 × 60
then the optimal number of operations is 4500 and this is achieved by multiplying the first
two matrices and then multiplying the resulting matrix with the third one.
1
The input to your function will consist of list giving the dimensions. If there are K matrices
in the sequence the list will contain K + 1 integers. For the above example, the input list will
be [10, 30, 5, 60].
Use m1, m2 . . . m10, m11 . . . etc to denote the K matrices. The first component of your
output will be the optimal number of operations to multiply the given matrices and the
second component will be a string giving a well-bracketed expression with . used to denote
matrix multiplication. For example
matmul [10,30,5,60] = (4500,"((m1.m2).m3)")
2