-
-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This guidance note provides instructions and suggestions for implementing solutions to the Collatz Conjecture problem in Python. It covers both a basic approach suitable for beginners and an optimized solution using memoization for improved efficiency. The note includes common suggestions, optimization tips, and talking points to enhance understanding and encourage experimentation.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
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. |