Skip to content
Snippets Groups Projects
Commit e89c05b4 authored by Alice Brenon's avatar Alice Brenon
Browse files

Add worksheet for practice 4

parent 5d9b9a16
No related branches found
No related tags found
No related merge requests found
---
title: Worksheet 4
---
# Memoization
## Naive Fibonacci
- As a reminder, the Fibonacci sequence is defined by
\begin{align*}
f_0 & = 1 \\
f_1 & = 1 \\
f_{n+2} & = f_{n+1} + f_n
\end{align*}
implement a function returning the `n`\textsuperscript{th} term of the
sequence with the following signature:
```Java
long fibonacci(int n);
```
- Test your function on "small" inputs ($< 10$). Now use it to find the value of
$f_{40}$ and note the time it takes. Try `45` if you're feeling particularly
daring today. What is going on ?
## Let's add some logging
- Modify the previous function to extract the sum (call to the operator `+`)
into a separate function. Check that everything is still working.
- Now modify the sum function to display both its arguments before returning its
result. Retry the small values. What do you notice ?
## Keeping answers
- Now we'll need a place to store terms, so let's create a class `Fibonacci`
with a `static` array named `cache` to hold all the terms already found. As
we've seen, terms around $f_{40}$ are already quite large, so we are likely to
hit the `long` overflow if we consider terms for indices too high so may want
to limit ourselves to a hundred terms, and dimension this cache
accordingly. Don't forget to initialize the first two cells of the cache for
$f_0$ and $f_1$ (hint: the value for this `static` field could be returned by
a `static` function).
- Add your previous functions as `static` methods of this new class, keep `sum`
and rename `fibonacci` into `get`.
- Now modify `get` to check if the result is missing from the cache first,
compute the sum and store it only in that case, and finally return the content
of the cache.
- Compute values you've computed before to check the results. What do you
observe regarding logging messages ? In particular, what happens when you
compute the same term twice ?
# Subtyping
## Wildcards
- We want to count the number of occurrences of elements in lists. Implement a
function which given a list `objects` and a particular `target` to look for in
the list returns an `int` value counting the number of occurrences of `target`
found within `objects`
```Java
int countOccurrences(List<Object> objects, Object target);
```
- Now declare a list of `Integers`, populate it with a couple values and test
your function `countOccurrences` on it. What happens ? Why ?
- Wildcards are helpful in this situation, replace `List<Object>` by `List<?>`
in the previous definition.
## Generics
- Test it again. Declare a `List<String>`, check that it works too. Can you look
for a `String` in the `List<Integer>` ? Is that a good thing ?
- Replace the wildcard by a generic type `T` to force the type of the list to
equal the type of the `target`. Try again to look for a `String` in a
`List<Integer>`. Better ?
## Bounds
- Now let's assume we want to count all elements smaller than the `target`. Our
function will hence work only on types which can be [compared](https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) now. Implement this change on the previous function.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment