From c3a0130f9a0e1c5845669838abc844448bc9fa57 Mon Sep 17 00:00:00 2001 From: Tomasz Gieorgijewski <24649931+Nerwosolek@users.noreply.github.com> Date: Mon, 5 Feb 2024 16:04:33 +0100 Subject: [PATCH 1/2] dict-and-join simplify --- .../practice/raindrops/.approaches/dict-and-join/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/raindrops/.approaches/dict-and-join/content.md b/exercises/practice/raindrops/.approaches/dict-and-join/content.md index ae302ada8dd..8ff87d04d2a 100644 --- a/exercises/practice/raindrops/.approaches/dict-and-join/content.md +++ b/exercises/practice/raindrops/.approaches/dict-and-join/content.md @@ -5,7 +5,7 @@ def convert(number): sounds = {3: 'Pling', 5: 'Plang', 7: 'Plong'} - results = ''.join(sounds[divisor] for + results = ''.join(sound for divisor, sound in sounds.items() if number % divisor == 0) From 8c8f759e402bb7e147a3804f88233dcee12d62af Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 5 Feb 2024 16:41:10 -0800 Subject: [PATCH 2/2] Swapped dictionary approaches Swapped the two dictionary approaches and adjusted `introduction.md`, `Benchmark.py`, `Performance`, and the approaches doc accordingly. --- .../.approaches/dict-and-join/content.md | 10 +++++----- .../.approaches/dict-and-join/snippet.txt | 2 +- .../raindrops/.approaches/introduction.md | 8 ++++---- .../.articles/performance/code/Benchmark.py | 15 +++++++++++++++ .../.articles/performance/content.md | 19 ++++++++++--------- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/exercises/practice/raindrops/.approaches/dict-and-join/content.md b/exercises/practice/raindrops/.approaches/dict-and-join/content.md index 8ff87d04d2a..94be03cb0a1 100644 --- a/exercises/practice/raindrops/.approaches/dict-and-join/content.md +++ b/exercises/practice/raindrops/.approaches/dict-and-join/content.md @@ -5,16 +5,16 @@ def convert(number): sounds = {3: 'Pling', 5: 'Plang', 7: 'Plong'} - results = ''.join(sound for - divisor, sound in sounds.items() + results = ''.join(sounds[divisor] for + divisor in sounds.keys() if number % divisor == 0) return results or str(number) ``` This approach uses a [dictionary][dict] called 'sounds' with factors as `keys` and sound strings as `values`. -A [generator-expression][generator-expressions] inside of [`str.join()`][str.join] loops through the [dictionary view object][dict-view-object] [`sounds.items()`][dict.items()], which is a series of (key, value) `tuples`. - Each `value` is looked up for every factor where number % divisor == 0. +A [generator-expression][generator-expressions] inside of [`str.join()`][str.join] loops through the [dictionary view object][dict-view-object] [`sounds.keys()`][dict.keys()], which is a sequence of all the dictionary keys. + Each `value` is looked up for every factor (key) where `number % divisor == 0`. `str.join()` then compiles the results. This is the equivalent of: @@ -46,7 +46,7 @@ def convert(number): ``` [dict-view-object]: https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects -[dict.items()]: https://docs.python.org/3/library/stdtypes.html#dict.items +[dict.keys()]: https://docs.python.org/3/library/stdtypes.html#dict.keys [dict]: https://docs.python.org/3/tutorial/datastructures.html#dictionaries [generator-expressions]: https://www.pythonmorsels.com/how-write-generator-expression/ [str.join]: https://docs.python.org/3/library/stdtypes.html#str.join diff --git a/exercises/practice/raindrops/.approaches/dict-and-join/snippet.txt b/exercises/practice/raindrops/.approaches/dict-and-join/snippet.txt index 6c24abb8df0..662c7c6e54e 100644 --- a/exercises/practice/raindrops/.approaches/dict-and-join/snippet.txt +++ b/exercises/practice/raindrops/.approaches/dict-and-join/snippet.txt @@ -2,7 +2,7 @@ def convert(number): sounds = {3: 'Pling', 5: 'Plang', 7: 'Plong'} results = ''.join(sounds[divisor] for - divisor, sound in sounds.items() + divisor in sounds.keys() if number % divisor == 0) return results or str(number) diff --git a/exercises/practice/raindrops/.approaches/introduction.md b/exercises/practice/raindrops/.approaches/introduction.md index b55ceabecbd..50e83d706ee 100644 --- a/exercises/practice/raindrops/.approaches/introduction.md +++ b/exercises/practice/raindrops/.approaches/introduction.md @@ -144,7 +144,7 @@ def convert(number): 7: 'Plong'} results = ''.join(sounds[divisor] for - divisor, sound in sounds.items() + divisor in sounds.keys() if number % divisor == 0) return results or str(number) @@ -248,8 +248,8 @@ def convert(number): 5: 'Plang', 7: 'Plong'} - results = (sounds[divisor] for - divisor in sounds.keys() + results = (sound for + divisor, sound in sounds.items() if number % divisor == 0) return ''.join(results) or str(number) @@ -258,7 +258,7 @@ def convert(number): This separates the code that calculates the results string from the factors themselves. If a factor needs to be added, only the dictionary needs to be touched. -This code need only iterate over the keys of the dictionary to do its calculation, making this `O(1)` in time complexity. +This code need only iterate over the items of the dictionary to do its calculation, making this `O(1)` in time complexity. This does take `O(m)` space, where `m` is equal to the number of factor entries. Since the number of factors is fixed here, this is unlikely to create issues unless a great many more are added to the 'sounds' `dict`. diff --git a/exercises/practice/raindrops/.articles/performance/code/Benchmark.py b/exercises/practice/raindrops/.articles/performance/code/Benchmark.py index a01978a7efe..b30a091e60c 100644 --- a/exercises/practice/raindrops/.articles/performance/code/Benchmark.py +++ b/exercises/practice/raindrops/.articles/performance/code/Benchmark.py @@ -63,6 +63,19 @@ def convert_dict(number): 7: 'Plong'} results = ''.join(sounds[divisor] for + divisor in sounds.keys() + if number % divisor == 0) + + return results or str(number) + + +def convert_dict_recommended(number): + + sounds = {3: 'Pling', + 5: 'Plang', + 7: 'Plong'} + + results = ''.join(sound for divisor, sound in sounds.items() if number % divisor == 0) @@ -127,6 +140,7 @@ def convert_pattern_matching(number): "loop with tuple", "sequence with join", "dictionary with join", + "dictionary recommended" "itertools with join", "functools reduce", "structural pattern matching"] @@ -140,6 +154,7 @@ def convert_pattern_matching(number): convert_loop, convert_sequence_join, convert_dict, + convert_dict_recommended, convert_itertools, convert_functools, convert_pattern_matching] diff --git a/exercises/practice/raindrops/.articles/performance/content.md b/exercises/practice/raindrops/.articles/performance/content.md index 522ff1016ee..7894d2f94c2 100644 --- a/exercises/practice/raindrops/.articles/performance/content.md +++ b/exercises/practice/raindrops/.articles/performance/content.md @@ -23,20 +23,21 @@ To benchmark these functions, we wrote a small [benchmarking script][benchmark-a | | 10 | 14 | 15 | 70 | 105 | 182 | 189 | 203 | 204 | 399 | 409 | 525 | 735 | 1575 | 3250 | |----------------------------- |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: |:--------: | -| if statements | 2.12e-07 | 2.16e-07 | 2.60e-07 | 2.76e-07 | 2.98e-07 | 2.10e-07 | 2.59e-07 | 2.15e-07 | 2.10e-07 | 2.75e-07 | 2.61e-07 | 3.41e-07 | 2.99e-07 | 2.98e-07 | 2.13e-07 | -| ternary with truthy/falsy | 2.65e-07 | 2.65e-07 | 2.80e-07 | 2.70e-07 | 2.91e-07 | 2.67e-07 | 2.73e-07 | 2.75e-07 | 2.69e-07 | 3.56e-07 | 2.02e-07 | 3.06e-07 | 3.06e-07 | 3.04e-07 | 2.89e-07 | -| loop with tuple | 4.01e-07 | 4.09e-07 | 5.05e-07 | 4.94e-07 | 6.48e-07 | 3.97e-07 | 5.25e-07 | 4.10e-07 | 2.04e-07 | 5.51e-07 | 4.06e-07 | 9.04e-07 | 6.16e-07 | 6.89e-07 | 4.33e-07 | -| structural pattern matching | 7.55e-07 | 7.31e-07 | 6.09e-07 | 5.87e-07 | 5.21e-07 | 7.11e-07 | 6.42e-07 | 7.19e-07 | 6.90e-07 | 6.49e-07 | 8.43e-07 | 5.00e-07 | 5.12e-07 | 5.21e-07 | 7.48e-07 | -| dictionary with join | 8.31e-07 | 8.18e-07 | 9.34e-07 | 1.02e-06 | 9.75e-07 | 8.55e-07 | 9.13e-07 | 8.25e-07 | 8.32e-07 | 2.28e-06 | 9.22e-07 | 1.05e-06 | 2.42e-06 | 9.94e-07 | 8.46e-07 | -| sequence with join | 8.29e-07 | 8.17e-07 | 9.27e-07 | 9.10e-07 | 9.62e-07 | 8.73e-07 | 9.70e-07 | 8.87e-07 | 9.40e-07 | 2.52e-06 | 9.74e-07 | 2.44e-06 | 2.57e-06 | 9.79e-07 | 8.67e-07 | -| itertools with join | 9.46e-07 | 9.33e-07 | 4.04e-07 | 9.88e-07 | 1.01e-06 | 9.41e-07 | 9.91e-07 | 9.65e-07 | 9.80e-07 | 2.51e-06 | 1.10e-06 | 2.50e-06 | 1.02e-06 | 1.00e-06 | 9.60e-07 | -| functools reduce | 1.35e-06 | 1.38e-06 | 1.41e-06 | 1.39e-06 | 1.48e-06 | 1.33e-06 | 1.42e-06 | 1.37e-06 | 1.34e-06 | 1.39e-06 | 1.43e-06 | 1.45e-06 | 1.46e-06 | 1.45e-06 | 1.37e-06 | +| if statements | 2.15e-07 | 2.13e-07 | 2.63e-07 | 2.62e-07 | 3.03e-07 | 2.13e-07 | 2.69e-07 | 2.12e-07 | 2.12e-07 | 2.75e-07 | 2.64e-07 | 3.04e-07 | 3.04e-07 | 3.06e-07 | 2.20e-07 | +| ternary with truthy/falsy | 2.79e-07 | 2.80e-07 | 2.89e-07 | 2.85e-07 | 3.02e-07 | 2.80e-07 | 3.13e-07 | 2.81e-07 | 2.79e-07 | 2.82e-07 | 3.30e-07 | 3.02e-07 | 3.02e-07 | 3.02e-07 | 2.80e-07 | +| loop with tuple | 7.91e-07 | 4.08e-07 | 5.07e-07 | 5.14e-07 | 6.13e-07 | 7.95e-07 | 5.10e-07 | 2.01e-07 | 8.18e-07 | 5.06e-07 | 3.85e-07 | 6.25e-07 | 6.10e-07 | 6.10e-07 | 2.00e-07 | +| structural pattern matching | 7.91e-07 | 7.39e-07 | 5.98e-07 | 6.24e-07 | 5.40e-07 | 7.47e-07 | 6.73e-07 | 7.72e-07 | 7.15e-07 | 6.69e-07 | 8.80e-07 | 5.43e-07 | 5.38e-07 | 5.69e-07 | 8.04e-07 | +| dictionary with join | 8.04e-07 | 7.98e-07 | 8.95e-07 | 9.81e-07 | 4.07e-07 | 8.15e-07 | 9.05e-07 | 8.14e-07 | 8.18e-07 | 9.05e-07 | 8.94e-07 | 9.51e-07 | 4.05e-07 | 9.80e-07 | 8.17e-07 | +| dictionary recommended | 8.55e-07 | 8.13e-07 | 8.99e-07 | 8.96e-07 | 9.36e-07 | 8.12e-07 | 9.27e-07 | 8.18e-07 | 8.24e-07 | 9.07e-07 | 9.13e-07 | 9.40e-07 | 9.36e-07 | 9.30e-07 | 8.15e-07 | +| sequence with  join | 8.59e-07 | 8.67e-07 | 9.56e-07 | 9.64e-07 | 4.04e-07 | 8.78e-07 | 9.65e-07 | 8.71e-07 | 8.74e-07 | 9.68e-07 | 9.27e-07 | 1.00e-06 | 1.02e-06 | 4.07e-07 | 8.59e-07 | +| itertools with join | 9.90e-07 | 9.82e-07 | 1.05e-06 | 1.03e-06 | 1.07e-06 | 9.85e-07 | 1.06e-06 | 9.83e-07 | 9.82e-07 | 1.04e-06 | 1.11e-06 | 1.07e-06 | 1.07e-06 | 1.04e-06 | 4.07e-07 | +| functools reduce | 1.42e-06 | 1.56e-06 | 1.45e-06 | 1.43e-06 | 1.50e-06 | 1.41e-06 | 1.46e-06 | 1.44e-06 | 1.41e-06 | 1.47e-06 | 1.49e-06 | 1.52e-06 | 1.49e-06 | 1.51e-06 | 1.43e-06 | Keep in mind that all these approaches are _very fast_, and that benchmarking at this granularity can be unstable. That caveat in mind, the two `if-statement` based approaches benchmark fastest, and the approach using `functools.reduce()` was the slowest. -The 'recommended' approach came in 4th, though it can be argued that the slowdown is justified by the increased readability and maintainability. +The 'dictionary with join' approach came in 5th, with the 'recommended dictionary' approach in 6th, though it can be argued that the slowdown for either dictionary approach is justified by the increased readability and maintainability. Measurements were taken on a 3.1 GHz Quad-Core Intel Core i7 Mac running MacOS Ventura. Tests used `timeit.Timer.autorange()`, repeated 3 times.