Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Near_ten
#1
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.
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
No, he didn't need 10. The possible results of x % n are 0 to n - 1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020