File: fibonacci.py

package info (click to toggle)
python-memray 1.17.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,396 kB
  • sloc: python: 28,451; ansic: 16,507; sh: 10,586; cpp: 8,494; javascript: 1,474; makefile: 822; awk: 12
file content (45 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import operator
from functools import reduce
from itertools import chain


def fibonacci(length):
    # edge cases
    if length < 1:
        return
    if length == 1:
        yield 1
        return

    left = right = 1
    yield left
    yield right

    for _ in range(length - 2):
        left, right = right, left + right
        yield right


def generate_fibonacci_hash(length_1, length_2, length_3):
    # We could have used sum(...) here instead of reduce(operator.add, ...),
    # but we choose to use reduce since it yields a more descriptive example
    # of the generated flamegraph for this specific example
    return (
        reduce(
            operator.add,
            chain(fibonacci(length_1), fibonacci(length_2), fibonacci(length_3)),
            0,
        )
        % 10000
    )


if __name__ == "__main__":
    # DO NOT CHANGE
    LENGTH_OF_SEQUENCE_1 = 33333
    LENGTH_OF_SEQUENCE_2 = 30000
    LENGTH_OF_SEQUENCE_3 = 34567
    # DO NOT CHANGE
    generate_fibonacci_hash(
        LENGTH_OF_SEQUENCE_1, LENGTH_OF_SEQUENCE_2, LENGTH_OF_SEQUENCE_3
    )