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

[python/square-root] Add mentoring doc #2338

Closed
wants to merge 3 commits into from

Conversation

antoniouaa
Copy link

Hello, my attempt at mentoring notes for square-root for python.
I see there's already been a past attempt at introducing this mentoring document , in #2251.

Document includes sections suggesting and explaining 2 different solutions, main solution using Heron's method from the wiki pages linked in the description of the problem.
I understand that's what the problem statement is pushing people to investigate and implement, instead of using cheap hacks like ** 0.5 and pow.

Comments welcome!

@github-actions github-actions bot added track/python Python track type/mentor-notes Mentor notes labels Apr 4, 2024

## Problem and Challenges

The problem asks us to find the square roots of numbers without using builtin functions or operators, like `**`, `pow`, `math.pow`, `math.sqrt` or `sum`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would sum not be allowed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly why, but it's mentioned in the exercise instructions as an example builtin to avoid.

Python offers a wealth of mathematical functions in the form of the math module and built-ins such as pow() and sum(). However, we'd like you to consider the challenge of solving this exercise without those built-ins or modules.

I can see at least 2 commmunity solutions where this function is used, inappropriately I might say:

  1. https://exercism.org/tracks/python/exercises/square-root/solutions/PetaGb
  2. https://exercism.org/tracks/python/exercises/square-root/solutions/soroushmz

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That instruction append is a nudge, as opposed to "absolutely not allowed". We're trying to (gently) discourage the x**.5 sort of solution (or at least prompt the student to do some hard thinking about it). However, if a student really doesn't want to do that -- it's their choice.

So maybe the mentor notes should have an example of how you'd use sum() in an appropriate way, or maybe some discussion points on how to move a student from the x**.5 solution toward a sort of intermediate solution, and eventually to the full Heron's.

What is the benefit of solving this problem "the long way around"? What could a student learn/get better at if they took on the challenge?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentoring notes can't really force a student to change their solution and I don't think the first couple lines suggest it absolutely shouldn't be done using **. Students could even hardcode values to fool the tests.

I imagine that people who asked for mentorship did so for 2 reasons:

  1. Their solution is too simple and are wondering if there's more to it,
  2. They tried implementing one of the methods in the wiki articles and want to improve on that implementation.

The spirit of the exercise is not to use a builtin operator, but to investigate other methods of doing the calculation without it.

Heron's method happens to be the simplest, its the first method linked in the wiki page, there's no intermediate step between ** or math.pow and that.

It's Heron's method that's the intermediary step, Newton's method and binary search (included as an alternative solution) follow the same path of shrinking the lookup space.

I could add the simpler solution of:

def square_root(number):
    for num in range(number+1):
        if num * num == number:
            return num

But this isn't really related to Heron's method, IMO they're distinct methods.

As for a sum() solution, I can't think of an appropriate one. The two sum() community solutions linked above sum just one number from the range. The snippet linked just above is arguably an improvement on it.

x_1 = (x_0 + 50 / x_0) / 2 # 13.5, not a very good revised estimate either
```

The power of Heron's method comes from how quickly it converges to a good approximation of a square root. In just 5 iterations, we get an estimation accurate to 6 decimal places!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercism markdown style is one sentence per line.

@antoniouaa antoniouaa closed this Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
track/python Python track type/mentor-notes Mentor notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants