我的下面的python代码在哪里失败?

时间:2016-10-12 04:09:44

标签: python primes

如果函数调用如下:backwardsPrime(9900, 10000)则输出应为[9923, 9931, 9941, 9967]。向后读取素数是在基数10(从右到左)向后读取的素数是不同的素数。它是Codewars中的kata之一,在提交以下解决方案时,我收到以下错误:

Traceback:
in File "./frameworks/python/cw-2.py", line 28, in assert_equals
expect(actual == expected, message, allow_raise)
File "./frameworks/python/cw-2.py", line 18, in expect
raise AssertException(message)
cw-2.AssertException: [1095047, 1095209, 1095319] should equal [1095047, 1095209, 1095319, 1095403]

代码:

import math
def isPrime(num):
    #flag = True
        rt = math.floor(math.sqrt(num))
        for i in range(2,int(rt)+1):
            if num % i == 0:
                return False

        else:
            return True    


    def Reverse_Integer(Number):
        Reverse = 0
        while(Number > 0):
            Reminder = Number %10
            Reverse = (Reverse *10) + Reminder
            Number = Number //10
        return Reverse

    def backwardsPrime(start, stop):
        s = list()

        for i in range(start,stop):
            if i>9 and i != Reverse_Integer(i):
                if isPrime(i) and isPrime(Reverse_Integer(i)):
                    s.append(i)
            else:
                continue
         return s

Codewars有自己的测试功能。下面的示例测试用例:

a = [9923, 9931, 9941, 9967]

test.assert_equals(backwardsPrime(9900, 10000), a)          

1 个答案:

答案 0 :(得分:0)

看起来您的代码在手动运行时通过了测试。也许他们扫描的范围在测试中设置错误导致它错过最后一个?

 backwardsPrime(1095000, 1095405)
 [1095047, 1095209, 1095319, 1095403]

e.g。第二个参数设置为1095400或其他。