![]() |
Near_ten - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Near_ten (/thread-23190.html) |
Near_ten - Dixon - Dec-15-2019 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. RE: Near_ten - ichabod801 - Dec-15-2019 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). RE: Near_ten - Dixon - Dec-16-2019 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 ![]() RE: Near_ten - ichabod801 - Dec-16-2019 No, he didn't need 10. The possible results of x % n are 0 to n - 1. |