generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 28
London | 25-SDC-NOV | Jesus del Moral | Sprint 2 | Improve with Precomputing #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
delmorallopez
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
delmorallopez:improve-with-precomputing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 20 additions & 11 deletions
31
Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,33 @@ | ||
| from typing import List | ||
| from functools import lru_cache | ||
|
|
||
|
|
||
| def find_longest_common_prefix(strings: List[str]): | ||
| """ | ||
| find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list. | ||
| def find_longest_common_prefix(strings: List[str]) -> str: | ||
| if len(strings) < 2: | ||
| return "" | ||
|
|
||
| # KEY INSIGHT: sort the strings. The longest common prefix between | ||
| # any two strings must appear between two adjacent strings when sorted. | ||
| # So we only need to check n-1 pairs instead of n² pairs. | ||
| sorted_strings = sorted(strings) | ||
|
|
||
| In the event that an empty list, a list containing one string, or a list of strings with no common prefixes is passed, the empty string will be returned. | ||
| """ | ||
| longest = "" | ||
| for string_index, string in enumerate(strings): | ||
| for other_string in strings[string_index+1:]: | ||
| common = find_common_prefix(string, other_string) | ||
| if len(common) > len(longest): | ||
| longest = common | ||
| for i in range(len(sorted_strings) - 1): | ||
| common = cached_find_common_prefix(sorted_strings[i], sorted_strings[i + 1]) | ||
| if len(common) > len(longest): | ||
| longest = common | ||
|
|
||
| return longest | ||
|
|
||
|
|
||
| def find_common_prefix(left: str, right: str) -> str: | ||
| @lru_cache(maxsize=None) | ||
| def cached_find_common_prefix(left: str, right: str) -> str: | ||
| min_length = min(len(left), len(right)) | ||
| for i in range(min_length): | ||
| if left[i] != right[i]: | ||
| return left[:i] | ||
| return left[:min_length] | ||
|
|
||
|
|
||
| def find_common_prefix(left: str, right: str) -> str: | ||
| return cached_find_common_prefix(left, right) | ||
1 change: 0 additions & 1 deletion
1
Sprint-2/improve_with_precomputing/common_prefix/common_prefix_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very clever approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
Sprint-2/improve_with_precomputing/count_letters/count_letters_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you justify the use of
@lru_cache? Do you expect it to always improve the performance?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the original version, we compare every string with every other string. If there are a lot of strings, the number of comparisons grows very quickly because it’s roughly proportional to the square of the number of strings. So doubling the number of strings makes the work grow by about four times.
In the new version, we first sort the strings. Sorting does add some work, but after sorting we only compare neighbouring strings instead of every possible pair. That reduces the number of prefix comparisons dramatically, from “compare everything with everything” to just “compare each string with the next one”.
As a result, the work now grows much more slowly as the number of strings increases. For large inputs, this makes a very noticeable difference in performance compared to the original approach.
So overall, the new implementation scales much better as the list gets bigger.
In this version, @lru_cache stores the result of cached_find_common_prefix(left, right) so that if the same pair of strings is compared again, we can return the result immediately instead of recomputing it.
In this specific implementation, we only compare each adjacent pair once after sorting. That means the same pair of strings is not normally recomputed. So in practice, the cache is unlikely to provide much benefit here.
So I have removed it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether lru_cache can help or not really depend on the characteristics of the strings we are comparing. Removing it is optional; it's more important to understand its pros and cons, which you did. Good job!