英文:
How to solve index out of range error in python?
问题
from sys import stdin
def arrayRotateCheck(arr, n):
i = 1
while arr[i] > arr[i - 1] and i < n - 1:
i += 1
if i == n - 1:
return 0
else:
return i
# Taking Input Using Fast I/O
def takeInput():
n = int(stdin.readline().rstrip())
if n == 0:
return list(), 0
arr = list(map(int, stdin.readline().rstrip().split()))
return arr, n
# main
t = int(stdin.readline().rstrip())
while t > 0:
arr, n = takeInput()
print(arrayRotateCheck(arr, n))
t -= 1
英文:
from sys import stdin
def arrayRotateCheck(arr, n):
i=1
while arr[i]>arr[i-1] and i<n-1:
i+=1
if i==n-1:
return 0
else:
return i
#Taking Input Using Fast I/O
def takeInput() :
n = int(stdin.readline().rstrip())
if n == 0:
return list(), 0
arr = list(map(int, stdin.readline().rstrip().split(" ")))
return arr, n
#main
t = int(stdin.readline().rstrip())
while t > 0 :
arr, n = takeInput()
print(arrayRotateCheck(arr, n))
t -= 1
This is a Python code finding array rotation index, the following line causing index out of range error. How to overcome this error.
while arr[i]>arr[i-1] and i<n-1:
答案1
得分: 0
在arrayRotateCheck函数中,当数组的长度为1时,程序的输出将是0。
以下是代码:
def arrayRotateCheck(arr, n):
i = 1
if len(arr) == 1:
return 0
while arr[i] > arr[i - 1] and i < n - 1:
i += 1
if i == n - 1:
return 0
else:
return i
英文:
you need to add a condition in arrayRotateCheck function, when the length of array is 1 then what will be the output of your program.
then here is code
def arrayRotateCheck(arr, n):
i = 1
if len(arr) == 1:
return 0
while arr[i] > arr[i - 1] and i < n - 1:
i += 1
if i == n - 1:
return 0
else:
return i
答案2
得分: 0
你的测试反过来了,所以它的第二部分没有像应该那样拯救你:
while arr[i] > arr[i-1] and i < n-1:
在验证i是否小于n - 1之前,它会从arr[i]中读取并进行测试。如果你颠倒测试组件,使之在验证索引之前不从arr中读取(并且用户输入不会出现错误,n永远不会为零;如它所示,这种设计有点傻,因为n是用户所声称的输入数量,但arr的长度不必以任何方式与之相符),你就不会越界:
while i < n-1 and arr[i] > arr[i-1]:
这会立即短路并在i大于或等于n - 1时不测试arr[i] > arr[i-1],从而退出循环。
另外,如果你从用户逐行接受输入,你不需要导入sys.stdin来访问它,你可以大大简化代码,用以下方式替换每个使用:
stdin.readline().rstrip()
只需使用:
input()
其中 input 从stdin中精确地读取一行,并为你删除换行符。我还建议调用.split()而不是.split(" ");后者如果用户在元素之间意外包含额外的空格,或者使用制表符,或者包含前导空格,将会表现不正常。前者将忽略所有前导和尾随空白,并在任何类型的空白的运行上分割,所以 1 2 3 会产生与 1 2 3 相同的结果。
英文:
Your test is backwards, so the second half of it doesn't save you the way it should:
while arr[i]>arr[i-1] and i<n-1:
is reading from and testing arr[i] before it's actually validated that i is less than n - 1. If you flip the test components so it doesn't read from arr until it's validated the index (and the inputs from the user aren't lies and n is never zero; as is, it's kind of a silly design, because n is the user's asserted number of inputs, but arr's length doesn't have to agree with that in any way), you won't go out of bounds:
while i < n-1 and arr[i] > arr[i-1]:
That short-circuits and doesn't even test arr[i] > arr[i-1] when i is greater than or equal to n - 1, exiting the loop immediately.
As a side-note, if you're accepting input a line at a time from the user, you don't need an import to gain access to sys.stdin, and you can simplify the code significantly, replacing each use of:
stdin.readline().rstrip()
with just:
input()
where input reads precisely one line from stdin, and strips the newline for you. I'd also recommend calling .split() instead of .split(" "); the latter will misbehave if the user accidentally includes additional spaces between elements, or uses tabs, or includes a leading space. The former will ignore all leading and trailing whitespace, and split on runs of any type of whitespace, so 1 2 3 will produce the same result as 1 2 3.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论