在Google IDE中测试我的代码时为什么会得到 RE (运行时错误)?
这是整个杂物箱: Google Kick Start subject
这是输入和输出指向我的Python代码的链接,可以正常工作:Check_here
这是我的代码的副本,以防您不想打扰外部链接。
def Solution(cases_nbr):
ans=[] # Output list
for case in range(cases_nbr):
dico={}
input() # this is not needed
# exmpl tmp_ls=10 7 4 6 8 10 11 will be -> 10 7 4 6 8 11 as int each
tmp_ls=sorted(set(map(int,input().split())))
for number in tmp_ls:
# create a new key (number) on dico which holds empty list.
dico[number]=[]
# loop through each uniq number in tmp_ls
for cmp_n in tmp_ls:
# check if the current cmp_n is not the current key (number)
# and also check if its not alreay a key in dico
if cmp_n!=number and cmp_n not in dico.keys():
# that will basically append the list : dico[key]
# with the Absolute diffrence between current number and cmp_n
dico[number].append(abs(number-cmp_n))
# at the end of this loop we will have something like :
# since the example i gave was 10 7 4 6 8 11 then the generated dico will be :
#{4: [2, 3, 4, 6, 7], 6: [1, 2, 4, 5], 7: [1, 3, 4], 8: [2, 3], 10: [1], 11: []}
#That way i will be able to find the longest contiguous arithmetic subarray
# by simply checking the most found value in each list in dico
tmp=[0,0]
# i take the first list since it has the all the substraction results
for diff in dico[list(dico.keys())[0]]:
# then i loop through each value(diff) and check if it exists in the rest of the lists
#tmp_count will end up having the count of diff in the other lists
tmp_count=len([1 for k in dico.keys() if diff in dico[k]])
# this if check if there is a longest count then it stores it in tmp[diff, count_val]
if tmp[1]<tmp_count:tmp=[diff, tmp_count]
# as we said
# the resulted dico is :
#{4: [2, 3, 4, 6, 7], 6: [1, 2, 4, 5], 7: [1, 3, 4], 8: [2, 3], 10: [1], 11: []}
#basically the result should be len([4 6 8 10]) because thats the longest contiguous arithmetic subarray
# However tmp now would have only : tmp[2,3] wheres it should have tmp[2,4]
# that why i loop again through the keys and check if there is no tmp[0] which is 'diff'
# in that dico[key] list , for example : 10: [1]
for _ in dico.keys():
if tmp[0] not in dico[_]:
# if so , i re-loop through [10 7 4 6 8 11] and test if that
# abs(x-key) is equal to tmp[0] , in this case :
# abs(10-6) is indeed == 2 , so i simply increment tmp[1]
tmp[1]+=len([_ for x in tmp_ls if x != _ and abs(x-_)==tmp[0] ])
# that way i will end up having the correct result .
# simply i append anser list with the formatted string output they said .
ans.append(f'Case #{case+1}: {tmp[1]}')
return ans
if __name__ == '__main__':
cases_nbr=int(input())
for ans in Solution(cases_nbr):
print(ans)