diff --git a/docs/_static/images/exercise1_flamegraph.png b/docs/_static/images/exercise1_flamegraph.png index 4f4f7966d1..6b05c05480 100644 Binary files a/docs/_static/images/exercise1_flamegraph.png and b/docs/_static/images/exercise1_flamegraph.png differ diff --git a/docs/tutorials/exercise_1/fibonacci.py b/docs/tutorials/exercise_1/fibonacci.py index a8e2bfe7ab..78d8ae17b3 100644 --- a/docs/tutorials/exercise_1/fibonacci.py +++ b/docs/tutorials/exercise_1/fibonacci.py @@ -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 + ) diff --git a/docs/tutorials/solutions/exercise_1/fibonacci.py b/docs/tutorials/solutions/exercise_1/fibonacci.py index fb31cb6e8b..0f168bb5c3 100644 --- a/docs/tutorials/solutions/exercise_1/fibonacci.py +++ b/docs/tutorials/solutions/exercise_1/fibonacci.py @@ -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 + ) diff --git a/docs/tutorials/tests/test_exercise_1.py b/docs/tutorials/tests/test_exercise_1.py index bb0a2956ce..1c81981d14 100644 --- a/docs/tutorials/tests/test_exercise_1.py +++ b/docs/tutorials/tests/test_exercise_1.py @@ -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