Python Forum

Full Version: Near_ten
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can someone explain the logic of the near_ten solution(s)on the codingbat website? Gregor Ulm has a solution that I really don't get. Then there's another that starts with a list: nums = [num-2, num-1, num, num+1, num+2] and goes into a for loop. Can't seem to wrap my old brain around it.
You are going to have to be more specific. I don't know what solutions you are talking about. Post the code you are having a problem understanding.

The list you give is all the numbers within two of the provided number. If any of those are a multiple of 10, then you would return True, otherwise you would return False (assuming you are talking about this problem). You could test each one with a for loop, which is what it sounds like is going on.

Another approach would just be to check num % 10. If num is within two of a power of ten, we know that num % 10 must be one of (0, 1, 2, 8, 9).
That's the problem ichabod. Gregor Ulm used 0,1,2,8,9 and 10 in his solution but I didn't understand it. Now I do thanks to your reply. He didn't need to include the 10 though Smile
No, he didn't need 10. The possible results of x % n are 0 to n - 1.