Skip to content
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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

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.


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.
Loading