我正在编写一个Python类,它检查给定的数字是否可以被数字2-11整除。我没有使用__init__
方法,在这种情况下,我认为这是不必要的。
问题:
abs(x)
方法的情况下将给定的数字转换为__init__
?class Divisibility:
"""
Program checks if given number is divisible
by any number from 2 to 11
"""
# Divisibe by 2 if last digit is divisible by 2
def divisible_by_2(self, number):
number = abs(number)
if (number % 10) % 2 == 0:
return True
else:
return False
# Divisible by 3 if sum of its digits is divisible by 3
def divisible_by_3(self, number):
number = abs(number)
n = number
s = 0
while n:
s, n = s + n % 10, n // 10
if s % 3 == 0:
return True
else:
return False
# Divisible by 4 if last two digits is divisible by 4
def divisible_by_4(self, number):
number = abs(number)
if (number % 100) % 4 == 0:
return True
else:
return False
# Divisible by 5 if last digit is either 5 or 0
def divisible_by_5(self, number):
number = abs(number)
if number % 10 == 0 or number % 10 == 5:
return True
else:
return False
# Divisible by 6 if number is divisible by 2 and 3
def divisible_by_6(self, number):
number = abs(number)
if self.divisible_by_2(number) and self.divisible_by_3(number):
return True
else:
return False
def divisible_by_7(self, number):
number = abs(number)
if number % 7 == 0:
return True
else:
return False
# Divisible by 8 if last three digits is divisible by 8
def divisible_by_8(self, number):
number = abs(number)
if (number % 1000) % 8 == 0:
return True
else:
return False
# Divisible by 9 if the sum of its digits is divisible by 9
def divisible_by_9(self, number):
number = abs(number)
s = 0
while number:
s, number = s + number % 10, number // 10
if s % 3 == 0:
return True
else:
return False
# Divisible by 10 if last digit is 0
def divisible_by_10(self, number):
number = abs(number)
if number % 10 == 0:
return True
else:
return False
# Divisible by 11 if the difference between
# the sum of numbers at even possitions and odd
# possitions is divisible by 11
def divisible_by_11(self, number):
number = abs(number)
n = number
nn = number // 10
total = 0
while n:
total += n % 10
n //= 100
while nn:
total -= nn % 10
nn //= 100
if abs(total) % 11 == 0:
return True
else:
return False
if __name__ == '__main__':
D = Divisibility()
print(D.divisible_by_2(-6))
print(D.divisible_by_3(6))
print(D.divisible_by_4(6))
print(D.divisible_by_5(60))
print(D.divisible_by_6(6))
print(D.divisible_by_7(616))
print(D.divisible_by_8(82453))
print(D.divisible_by_9(9512244))
print(D.divisible_by_10(-571288441))
print(D.divisible_by_11(121))
发布于 2018-09-22 23:38:23
有个程序员在里面开玩笑说:“没有一个有自尊心的程序员会同意写一个炸弹巴格达函数,他们会写一个炸弹城市函数,巴格达可以作为参数传递给它。”
这是这里的代码的最大问题:所有的可分性函数都可以与要检查的作为参数传递的数字合并为一个。这也解决了你正确地提出的关于腹肌的问题。
特别要注意的是,计算机存储数字的方式与我们编写数字的方式不同,因此,虽然像检查最后一个数字的均匀度这样的快捷键对我们来说更容易和更快,但计算机发现最后一个数字是什么要比只检查整个数字和两个数字更慢!因此,对所有这些都使用相同的样式与对七种使用相同的样式是很好的。这意味着合并它们没有任何障碍。(从技术上讲,对于2、4和8有一些更快的选项,但在每种情况下代码都是相同的,这比使用它们更重要。)
最后,我喜欢你写下了一些测试用例。我建议有更有系统地这样做的余地。确保你包括一些是的,一些不同意的,以及任何可能引起麻烦的特殊情况。(负数、零、数字本身等)为每个函数编写单独的代码。例如,我认为9函数可以用更多的刺激来完成。举个例子,试着扔6个。)(不是为了强调这一点,而是简化测试,以确保一切都是正确的,这是使用通用函数的关键原因。)
发布于 2018-09-22 22:40:20
你经常这么做
if s % 3 == 0:
return True
else:
return False
相反,您可以直接返回
return s % 3 == 0
发布于 2018-09-23 15:03:28
如果你追求的是速度,最快是最简单的:
def divisible(number, divisor):
return number % divisor == 0
https://codereview.stackexchange.com/questions/204206
复制相似问题