谷歌编码堵塞b轮2019建筑广场不断抛出运行时错误



occ((返回1,如果可以转换为普通机场,则返回0

def occ(A):
odd = 0
for a in A:
if a % 2 != 0:  # is odd
odd += 1
if odd > 1:
return 0
return 1

主要功能

p = int(input())
for f in range(1,p+1):
n,q = map(long,input().split())
sample= input()
c=0
for i in range(q):
s,e = map(int,input().split())
c=c+occ(sample[s-1:e+1])
print("Case #"+str(f)+": "+str(c))
  1. long(n)不是函数,int(n)是函数。Python在以下错误消息中告诉您这一点:
s,e = map(long,input().split())
NameError: name 'long' is not defined    <-- HERE it says "long" does not exist

在Python中,int没有限制,因此不需要long数据类型。该行应更改为:

s,e = map(int,input().split())
  1. 样本子序列不正确。目前,它是sample[s-1:e+1],但应该是sample[s-1:e]。在一张纸上试一下,自己检查一下。

  2. 您发布的occ(A)方法不完整。你不想检查每个字母是奇数还是偶数,这没有意义。您要检查子字符串中是否只有一个字母出现奇数次。下面是一个修复程序。

def occ(substring):
letter_count = [0 for _ in range(26)]
for c in substring:
letter_count[ord(c) - ord("A")] += 1
# The code below is the original occ
odd = 0
for a in letter_count:
if a % 2 != 0:
odd += 1
if odd > 1:
return 0
return 1

相关内容

最新更新