HackerRank 动态数组问题 - 代码中的运行时错误
HackerRank Dynamic Array Problem - Runtime Error in the code
我正尝试在 HackerRank 上解决这个 Dynamic Array problem。这是我的代码:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'dynamicArray' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER n
# 2. 2D_INTEGER_ARRAY queries
#
def dynamicArray(n, queries):
lastAnswer = 0
a = []
array_result = []
for k in range(n):
a.append([])
for i in queries:
x = i[1]
y = i[2]
if i[0] == 1:
seq = ((x ^ lastAnswer) % n)
a[seq].append(y)
elif i[0] == 2:
seq = ((x ^ lastAnswer) % n)
lastAnswer = a[seq][y]
array_result.append(lastAnswer)
return array_result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
q = int(first_multiple_input[1])
queries = [] # 1 0 5, 1 1 7, 1 0 3, ...
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
result = dynamicArray(n, queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
我收到运行时错误:
Traceback (most recent call last):
File "Solution.py", line 50, in
fptr.write('\n'.join(map(str, result)))
TypeError: 'NoneType' object is not iterable
谁能帮我解决这个问题,我似乎找不到解决办法。
这是输入:
2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
谢谢。
Update: It seems like this input is working now, thanks to @cireo but the code is not working for other test cases. What the problem with this code?
您的问题的答案在于 hackerrank 提供的样板。
# The function is expected to return an INTEGER_ARRAY.
您还可以看到 result = dynamicArray(n, queries)
需要 return 来自 map(str, result)
的整数列表,这会引发异常。
在你的代码中你做了 print(lastAnswer)
,但你可能想要
+ ret = []
...
- print(lastAnswer)
+ ret.append(lastAnswer)
+ return ret
相反。
因为你没有return任何东西,默认情况下函数returns None
不能被map
迭代。
你可以试试这个,它工作得很好。(没有运行时错误)
Replace your dynamicArray function with this code. Hopefully this will be helpful for you (^_^).
def dynamicArray(n, 查询):
col = [[] for i in range(n)]
res = []
lastanswer = 0
for q in queries:
data = (q[1]^lastanswer)%n
if q[0] == 1:
col[data].append(q[2])
elif q[0] == 2:
ind_x = q[2]%len(col[data])
lastanswer = col[data][ind_x]
res.append(lastanswer)
return res
我正尝试在 HackerRank 上解决这个 Dynamic Array problem。这是我的代码:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'dynamicArray' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER n
# 2. 2D_INTEGER_ARRAY queries
#
def dynamicArray(n, queries):
lastAnswer = 0
a = []
array_result = []
for k in range(n):
a.append([])
for i in queries:
x = i[1]
y = i[2]
if i[0] == 1:
seq = ((x ^ lastAnswer) % n)
a[seq].append(y)
elif i[0] == 2:
seq = ((x ^ lastAnswer) % n)
lastAnswer = a[seq][y]
array_result.append(lastAnswer)
return array_result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
q = int(first_multiple_input[1])
queries = [] # 1 0 5, 1 1 7, 1 0 3, ...
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
result = dynamicArray(n, queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
我收到运行时错误:
Traceback (most recent call last):
File "Solution.py", line 50, in
fptr.write('\n'.join(map(str, result)))
TypeError: 'NoneType' object is not iterable
谁能帮我解决这个问题,我似乎找不到解决办法。
这是输入:
2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
谢谢。
Update: It seems like this input is working now, thanks to @cireo but the code is not working for other test cases. What the problem with this code?
您的问题的答案在于 hackerrank 提供的样板。
# The function is expected to return an INTEGER_ARRAY.
您还可以看到 result = dynamicArray(n, queries)
需要 return 来自 map(str, result)
的整数列表,这会引发异常。
在你的代码中你做了 print(lastAnswer)
,但你可能想要
+ ret = []
...
- print(lastAnswer)
+ ret.append(lastAnswer)
+ return ret
相反。
因为你没有return任何东西,默认情况下函数returns None
不能被map
迭代。
你可以试试这个,它工作得很好。(没有运行时错误)
Replace your dynamicArray function with this code. Hopefully this will be helpful for you (^_^).
def dynamicArray(n, 查询):
col = [[] for i in range(n)]
res = []
lastanswer = 0
for q in queries:
data = (q[1]^lastanswer)%n
if q[0] == 1:
col[data].append(q[2])
elif q[0] == 2:
ind_x = q[2]%len(col[data])
lastanswer = col[data][ind_x]
res.append(lastanswer)
return res