near_ten() 在 Codingbat 上似乎有错误

near_ten() appears to have a bug on Codingbat

Codingbat 在 Logic-1 下有一道练习题,Python。它被称为near_10。

Given a non-negative number "num", return True if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2

一个用户GitHub的解决方案是

def near_ten(num):
  
  within = num%((num/10)*10) if num >= 10 else num
  return within in [8,9,0,1,2]

这被接受为代码检查中包含的所有示例的正确答案 table

但是,在 Jupyter Notebook 中,当我在函数 near_ten() 中传递上述 table 中的 False 示例时,我得到了不同的结果

我在 S.O 上搜索过这里。并通过 UltraInstinct 找到了有效的答案

您提供的解决方案确实是错误的,根本不起作用。看一下第一项 ((num/10)*10):它的计算结果仅为 num。这意味着对于所有大于 9 的整数,您的结果是 num % num,它始终为 0。同样,因为此代码使用正常除法,所以它将所有项转换为浮点数。

您提供的第二个解决方案是正确的,因为您只需要第一个非十进制数字。