Skip to main content
Tweeted twitter.com/StackCodeReview/status/935687705245245440
deleted 2 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Feels like my code Testing whether a number is digressivenearly a multiple of 10

I'm going through a basic python exercise, stumbled upon this query.

 

Given a non-negative number "num", return True if num is within 2 of a multiple of 10.

Given a non-negative number "num", return True if num is within 2 of a multiple of 10.

Here's the my solution:

def near_ten(num):
  return (num % 10) in range(8, 10) or (num % 10) in range(0, 3)

It shows "All Correct", but I'm feeling like my code is bit digressive as I have hard-coded the "numbers within 2 of a multiple of 10".

Can one please review this line if its the right approach?

Feels like my code is digressive

I'm going through a basic python exercise, stumbled upon this query.

 

Given a non-negative number "num", return True if num is within 2 of a multiple of 10.

Here's the my solution:

def near_ten(num):
  return (num % 10) in range(8, 10) or (num % 10) in range(0, 3)

It shows "All Correct", but I'm feeling like my code is bit digressive as I have hard-coded the "numbers within 2 of a multiple of 10".

Can one please review this line if its the right approach?

Testing whether a number is nearly a multiple of 10

I'm going through a basic python exercise, stumbled upon this query.

Given a non-negative number "num", return True if num is within 2 of a multiple of 10.

Here's the my solution:

def near_ten(num):
  return (num % 10) in range(8, 10) or (num % 10) in range(0, 3)

It shows "All Correct", but I'm feeling like my code is bit digressive as I have hard-coded the "numbers within 2 of a multiple of 10".

Can one please review this line if its the right approach?

Source Link
trex
  • 161
  • 4

Feels like my code is digressive

I'm going through a basic python exercise, stumbled upon this query.

Given a non-negative number "num", return True if num is within 2 of a multiple of 10.

Here's the my solution:

def near_ten(num):
  return (num % 10) in range(8, 10) or (num % 10) in range(0, 3)

It shows "All Correct", but I'm feeling like my code is bit digressive as I have hard-coded the "numbers within 2 of a multiple of 10".

Can one please review this line if its the right approach?