-
-
Notifications
You must be signed in to change notification settings - Fork 946
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
Create mentoring.md #2340
Closed
Closed
Create mentoring.md #2340
Changes from all commits
Commits
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
88 changes: 88 additions & 0 deletions
88
tracks/python/exercises/collatz-conjecture/collatz-conjecture/mentoring.md
This file contains 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
### Collatz Conjecture Solution Guidance Note | ||
|
||
--- | ||
|
||
#### Reasonable Solutions | ||
|
||
A simple and clear solution for solving the Collatz Conjecture is presented below: | ||
|
||
```python | ||
def steps(number): | ||
if not isinstance(number, int) or number <= 0: | ||
raise ValueError("Only positive integers are allowed") | ||
|
||
step_count = 0 | ||
|
||
while number != 1: | ||
if number % 2 == 0: | ||
number //= 2 | ||
else: | ||
number = 3 * number + 1 | ||
step_count += 1 | ||
|
||
return step_count | ||
``` | ||
|
||
This solution is straightforward and easy to understand. It uses basic control flow structures and handles input validation, making it a good starting point for beginners. | ||
|
||
--- | ||
|
||
#### Optimized Solution | ||
|
||
For those interested in optimizing the solution, memoization can significantly improve the efficiency by storing previously computed results. | ||
|
||
```python | ||
def steps(number, memo={}): | ||
if number <= 0: | ||
raise ValueError("Only positive integers are allowed") | ||
|
||
original_number = number | ||
count = 0 | ||
|
||
while number != 1: | ||
if number in memo: | ||
count += memo[number] | ||
break | ||
if number & 1 == 0: | ||
number >>= 1 | ||
else: | ||
number = 3 * number + 1 | ||
count += 1 | ||
|
||
memo[original_number] = count | ||
return count | ||
``` | ||
|
||
This optimized version uses a dictionary to store the number of steps for each number encountered, reducing redundant calculations and speeding up the process, especially for larger numbers. | ||
|
||
--- | ||
|
||
#### Common Suggestions | ||
|
||
1. **Handling Input:** | ||
- Always validate input to ensure it's a positive integer. | ||
- Raise an appropriate exception with a clear error message if the input is invalid. | ||
|
||
2. **Optimization Tips:** | ||
- **Memoization**: Store intermediate results to avoid recalculating steps for the same numbers. | ||
- **Bitwise Operations**: Use bitwise operators for faster calculations (e.g., `number & 1` instead of `number % 2`). | ||
|
||
3. **Efficiency Considerations:** | ||
- Memoization significantly improves efficiency, especially with larger input numbers. | ||
- Bitwise operations can provide a slight performance boost. | ||
|
||
--- | ||
|
||
#### Talking Points | ||
|
||
1. **Memoization Benefits:** | ||
- Explain how memoization helps in avoiding redundant calculations by storing previously computed results. | ||
- Discuss how this can lead to significant performance improvements for large inputs. | ||
|
||
2. **Bitwise Operations:** | ||
- Introduce the use of bitwise operations for checking evenness and performing divisions by 2. | ||
- Highlight the efficiency gained from using these low-level operations. | ||
|
||
3. **Code Readability:** | ||
- Emphasize the importance of clear and readable code, especially when implementing optimizations. | ||
- Encourage the use of meaningful variable names and comments to explain the logic. |
Oops, something went wrong.
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.
These notes seem very biased towards pushing people towards adding memorization. It's that always faster? Is that faster when using the test inputs? It would be good to explore both the benefits and drawbacks - provide a rounded analysis.
If you want to explore more options, using a
for steps on itertools.count()
can be interesting.