Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/_static/images/exercise1_flamegraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 49 additions & 45 deletions docs/tutorials/exercise_1/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
import operator
from functools import reduce
from itertools import chain


def fibonacci(length):
# edge cases
if length < 1:
return []
if length == 1:
return [1]
if length == 2:
return [1, 1]

output = [1, 1]

for i in range(length - 2):
output.append(output[i] + output[i + 1])

return output


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
)
import operator
from functools import reduce
from itertools import chain


def fibonacci(length):
# edge cases
if length < 1:
return []
if length == 1:
return [1]
if length == 2:
return [1, 1]

output = [1, 1]

for i in range(length - 2):
output.append(output[i] + output[i + 1])

return output


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 = 11111
LENGTH_OF_SEQUENCE_2 = 22222
LENGTH_OF_SEQUENCE_3 = 33333
# DO NOT CHANGE
generate_fibonacci_hash(
LENGTH_OF_SEQUENCE_1, LENGTH_OF_SEQUENCE_2, LENGTH_OF_SEQUENCE_3
)
94 changes: 49 additions & 45 deletions docs/tutorials/solutions/exercise_1/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
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
)
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 = 11111
LENGTH_OF_SEQUENCE_2 = 22222
LENGTH_OF_SEQUENCE_3 = 33333
# DO NOT CHANGE
generate_fibonacci_hash(
LENGTH_OF_SEQUENCE_1, LENGTH_OF_SEQUENCE_2, LENGTH_OF_SEQUENCE_3
)
112 changes: 56 additions & 56 deletions docs/tutorials/tests/test_exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import pytest
from exercise_1.fibonacci import generate_fibonacci_hash

# Memory tests


@pytest.mark.limit_memory("100 KB")
def test_fibonacci():
LENGTH_OF_SEQUENCE_1 = 33333 # pylint: disable=invalid-name
LENGTH_OF_SEQUENCE_2 = 30000 # pylint: disable=invalid-name
LENGTH_OF_SEQUENCE_3 = 34567 # pylint: disable=invalid-name

generate_fibonacci_hash(
LENGTH_OF_SEQUENCE_1, LENGTH_OF_SEQUENCE_2, LENGTH_OF_SEQUENCE_3
)


# Correctness tests


def test_fibonacci_empty():
h = generate_fibonacci_hash(0, 0, 0)
assert h == 0


@pytest.mark.parametrize(
("length", "expected"),
[
(1, 1),
(2, 2), # 1 + 1
(6, 20), # 1 + 1 + 2 + 3 + 5 + 8
],
)
def test_fibonacci_short(length, expected):
h = generate_fibonacci_hash(0, 0, length)
assert h == expected


@pytest.mark.parametrize(
("length", "expected"),
[
(1, 1),
(2, 2), # 1 + 1
(6, 20), # 1 + 1 + 2 + 3 + 5 + 8
],
)
def test_fibonacci_multiple(length, expected):
h = generate_fibonacci_hash(length, length, length)
assert h == expected * 3


def test_hash_modulo_10000():
# 1 + 1 + 2 +3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 + 233 + 377 + 610
# + 987 + 1597 + 2584 + 4181 == 10945
h = generate_fibonacci_hash(0, 0, 19)
assert h == 945 # 10945 % 10000
import pytest
from exercise_1.fibonacci import generate_fibonacci_hash
# Memory tests
@pytest.mark.limit_memory("100 KB")
def test_fibonacci():
LENGTH_OF_SEQUENCE_1 = 11111 # pylint: disable=invalid-name
LENGTH_OF_SEQUENCE_2 = 22222 # pylint: disable=invalid-name
LENGTH_OF_SEQUENCE_3 = 33333 # pylint: disable=invalid-name
generate_fibonacci_hash(
LENGTH_OF_SEQUENCE_1, LENGTH_OF_SEQUENCE_2, LENGTH_OF_SEQUENCE_3
)
# Correctness tests
def test_fibonacci_empty():
h = generate_fibonacci_hash(0, 0, 0)
assert h == 0
@pytest.mark.parametrize(
("length", "expected"),
[
(1, 1),
(2, 2), # 1 + 1
(6, 20), # 1 + 1 + 2 + 3 + 5 + 8
],
)
def test_fibonacci_short(length, expected):
h = generate_fibonacci_hash(0, 0, length)
assert h == expected
@pytest.mark.parametrize(
("length", "expected"),
[
(1, 1),
(2, 2), # 1 + 1
(6, 20), # 1 + 1 + 2 + 3 + 5 + 8
],
)
def test_fibonacci_multiple(length, expected):
h = generate_fibonacci_hash(length, length, length)
assert h == expected * 3
def test_hash_modulo_10000():
# 1 + 1 + 2 +3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 + 233 + 377 + 610
# + 987 + 1597 + 2584 + 4181 == 10945
h = generate_fibonacci_hash(0, 0, 19)
assert h == 945 # 10945 % 10000
Loading