Python 为什么程序中存在运行时错误?

Python 为什么程序中存在运行时错误?,python,testing,palindrome,Python,Testing,Palindrome,在HackerRank上的一个问题中,回文子串的数量将被计数 该程序运行良好,使用不同的测试用例进行了测试 但是它没有通过HackerRank上的最后两个测试用例 我的程序未能成功执行的可能测试用例有哪些 这是问题陈述 它有一个参数:一个字符串,s。它必须返回一个整数,表示s的回文子字符串的数量。 约束 def countPalindromes(s): counter=0 length = len(s) list1= ([s[i:j+1] for i in range(l

在HackerRank上的一个问题中,回文子串的数量将被计数

该程序运行良好,使用不同的测试用例进行了测试

但是它没有通过HackerRank上的最后两个测试用例

我的程序未能成功执行的可能测试用例有哪些

这是问题陈述

它有一个参数:一个字符串,s。它必须返回一个整数,表示s的回文子字符串的数量。

约束

def countPalindromes(s):
    counter=0
    length = len(s)
    list1= ([s[i:j+1] for i in range(length) for j in range(i,length)])
    list2=([x[::-1] for x in list1])
    for i in range(len(list2)):
        if(list1[i]==list2[i]):
            counter+=1
    return counter

#input = aaa
#output = 6 i.e. {a,a,a,aa,aa,aaa}
#input = abccba
#output = 9
#input = daata
#output = 7
#output is correct though failing the last 2 test cases
1≤ |S|≤ 5×(10)^3 s由小写英文字母组成。

输出格式

函数必须返回一个整数,表示s的不同回文子字符串的数量

def countPalindromes(s):
    counter=0
    length = len(s)
    list1= ([s[i:j+1] for i in range(length) for j in range(i,length)])
    list2=([x[::-1] for x in list1])
    for i in range(len(list2)):
        if(list1[i]==list2[i]):
            counter+=1
    return counter

#input = aaa
#output = 6 i.e. {a,a,a,aa,aa,aaa}
#input = abccba
#output = 9
#input = daata
#output = 7
#output is correct though failing the last 2 test cases
可以使用和计算字符串中回文子字符串的数量。例如:

def is_palindrome(s):
    return 1 if s == s[::-1] else 0


def count_palindromes(s, i, j, m):
    key = '{}-{}'.format(i, j)
    if key in m.keys():
        return m[key]
    if i == len(s)-1 or i > j:
        return 0
    m[key] = is_palindrome(s[i:j]) + count_palindromes(s, i+1, j, m) + count_palindromes(s, i, j-1, m)
    return m[key]


m = {}
print(count_palindromes('aaa', 0, 2, m)) # should be count_palindromes(s, 0, len(s)-1, m)
# output: 6

函数应该做什么?计算回文子串的总数。请看末尾的注释。在编写代码时,您肯定有更详细的规范。现在不可能看出它有什么问题。挑战的名称是什么?提供一个链接或指向黑客排名网站的东西。