如何解决当且仅当一个数字具有至少两个连续的数字且每个数字等于3或该数字的最后一个数字可被4整除时,该数字才是苍白的
def pale(n):
'''(int)->bool
return A number is not pale if and only if it has at least two consecutive digit divisible by 4.
>>> pale(1128)
Fasle
>>> pale(3443)
True
'''
return n%4!=0 and n!=33
print(pale(5433))
但是答案5433中的某些错误是错误的,但答案是正确的。请推荐给我。
解决方法
您必须查看n的每个数字,而不是整数:
def pale(n):
sym = str(n)
for idx in range(len(sym)-1): # For each digit in the number n
if int(sym[idx])%4 == 0: # if the digit is divisible by 4
if int(sym[idx+1])%4 == 0: # and if the next one is divisible by 4
return True
return False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。