https://github.com/zmuhls/ling78100-mp3
Machine programming assignment on bigrams and functions
https://github.com/zmuhls/ling78100-mp3
bigrams computational-linguistics fibonacci-sequence functions python
Last synced: 3 months ago
JSON representation
Machine programming assignment on bigrams and functions
- Host: GitHub
- URL: https://github.com/zmuhls/ling78100-mp3
- Owner: zmuhls
- Created: 2020-01-09T08:42:10.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-09T08:54:58.000Z (over 5 years ago)
- Last Synced: 2025-01-20T20:52:40.011Z (5 months ago)
- Topics: bigrams, computational-linguistics, fibonacci-sequence, functions, python
- Language: Python
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
MP2: Functions
==============This MP consists of two parts. Both parts are also commonly used as "screening"
questions in technical interviews.Fibonacci numbers
=================The [*Fibonacci numbers*](https://en.wikipedia.org/wiki/Fibonacci_number) are a
integer sequence such that each number is the sum of the two preceding numbers.By convention, the "zero"th number in the sequence is 0, and the "first" number
is 1, respectively. Thus the sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, ...### What to do
Write a function of the form:
```python
def fib(n: int) -> int:
pass # FIXME.
```which `return`s the `n`th Fibonacci number where `n` is a non-negative integer.
Then, test it (using `assert`) for 1, 2, 3, and 100.
### Test support
The 100th Fibonacci number is 354224848179261915075.
### Hints
- First compute the `n`th Fibonacci number, then `return` it (don't `print`
it).
- Do not try to define this recursively; it will be too slow for `fib(100)`.### Stretch goal
Write a infinite generator function of the form:
```python
from typing import Iteratordef fib_generator() -> Iterator[int]:
pass # FIXME
```which generates the infinite Fibonacci sequence. Then show it is correct up to
100 using the following snippet:```python
gen = fib_generator()
for i in range(100):
assert next(gen) == fib(i)
```Bigrams
=======A *bigram* is a ordered pair of contiguous symbols (usually, but not
necessarily) words. For instance, the previous sentence contains bigrams such as
`["of", "contiguous"]`, and `["but", "not"]`, but not `["is", "ordered"]`
(because those words are not contiguous) or `["a", "is"]` (because those words
do not occur in that order).### What to do
Write a function of the form:
```python
from typing import Listdef bigrams(sequence: List[str]) -> List[List[str]]:
pass # FIXME
```which returns all bigrams (in order) from `sequence` (a list of strings). If
`sequence` is empty, or of length one, it should return nothing, but crucially,
your function **should not** crash.### Test support
Your implementation should pass the following assertion tests:
```python
# Empty list.
tokens = []
assert not bigrams(tokens)# List with one element,
tokens = ["Hi"]
assert not bigrams(tokens)# List with many elements.
tokens = ["Four", "score", "and", "seven", "years"]
assert bigrams(tokens) == [["Four", "score"],
["score", "and"],
["and", "seven"],
["seven", "years"]]
```### Stretch goal
Write a generator function of the form:
```python
from typing import List, Iterabledef ngram_generator(sequence: List[str], n: int = 2) -> Iterable[List[str]]:
pass # FIXME
```where `n` is the "order" of the bigrams (e.g., n = 2 gives bigrams, n = 3 gives
trigrams). Then, write some basic assertion tests to show its correctness.