Python 3.x Google Kickstart 2020第E轮运行时错误
为什么在GoogleIDE中测试代码时会出现RE(运行时错误) 这是整个pastbin: 下面是我的Python代码的链接,其中包含输入和输出,运行良好: 这里是我的代码的副本,以防你不想麻烦检查一个外部链接Python 3.x Google Kickstart 2020第E轮运行时错误,python-3.x,runtime-error,Python 3.x,Runtime Error,为什么在GoogleIDE中测试代码时会出现RE(运行时错误) 这是整个pastbin: 下面是我的Python代码的链接,其中包含输入和输出,运行良好: 这里是我的代码的副本,以防你不想麻烦检查一个外部链接 def Solution(cases_nbr): ans=[] # Output list for case in range(cases_nbr): dico={} input() # this is not needed
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)
def溶液(案例编号):
ans=[]输出列表
对于范围内的案例(案例编号):
dico={}
input()#这是不需要的
#exmpl tmp_ls=10 7 4 6 8 10 11将->10 7 4 6 8 11作为int
tmp_ls=sorted(set(map(int,input().split()))
对于tmp_ls中的编号:
#在保存空列表的dico上创建一个新密钥(编号)。
dico[编号]=[]
#通过tmp_ls中的每个uniq编号循环
对于tmp中的cmp\u n:
#检查当前cmp\n是否不是当前密钥(编号)
#同时也要检查它是否真的是dico中的一个键
如果是的话=数字和cmp\n不在dico.keys()中:
#这将基本上附加列表:dico[key]
#在当前数量和cmp\n之间存在绝对差异
dico[number].追加(abs(number-cmp\n))
#在这个循环的最后,我们将有如下内容:
#由于我给出的示例是10 7 4 6 8 11,因此生成的dico将是:
#{4: [2, 3, 4, 6, 7], 6: [1, 2, 4, 5], 7: [1, 3, 4], 8: [2, 3], 10: [1], 11: []}
#这样我就可以找到最长的连续算术子数组
#只需检查dico中每个列表中找到最多的值
tmp=[0,0]
#我选择第一个列表,因为它包含所有的减法结果
对于dico[列表(dico.keys())[0]]中的差异:
#然后我循环遍历每个值(diff),并检查它是否存在于列表的其余部分中
#tmp_count最终会在其他列表中包含diff的计数
tmp_count=len([1表示dico中的k.keys(),如果dico中的diff[k]]))
#此if检查是否存在最长计数,然后将其存储在tmp[diff,count\u val]
如果tmp[1],我看不到可能发生错误的明显位置。您是否可以提供更详细的错误消息?您能否提供此代码试图解决的问题的描述?你能用注释来说明你的代码在每一步都做了什么,这样我们在搜索错误的原因时更容易理解吗?@GreenCoveGuy Done(我有时不擅长解释代码),请重新检查。