英文:
Why do i get a list index out of range error with entries with a length of four digits?
问题
I wrote this algorithm that returns the shortest sequence using operations to sort a list of numbers.
is sorted by always reversing the order of the first n elements of the list and removing the first element from the list.
def wenden(arr, n):
# Reverses the first n elements
i = 0
while i < n // 2:
arr[i], arr[n - i-1] = arr[n - i-1], arr[i]
i += 1
arr.pop(0)
return arr
def ist_Sortirt(arr):
for i in range(len(arr) - 1):
if arr[i] > arr[i+1]:
return False
return True
def best_sort(arr):
flips = []
if not ist_Sortirt(arr):
for i in range(1, len(arr)-1):
sort_lis = wenden(arr, i + 1)
flipi = [i+1]
flipi.append(best_sort(sort_lis))
flips.append(flipi)
ret = 0
for i in range(1, len(flips)-1):
if len(flips[i]) < len(flips[ret]):
ret = i
return flips[ret]
else:
return []
I have tested the script with different input lists, a list with the phrases should be output, but I always get a list index out of range error at return flips[ret]
in best_sort()
if the input is longer than 3 numbers
input: [2,1,3,6]
Traceback (most recent call last):
File "D:\programming\Aufgabe3a.py", line 69, in <module>
print(best_sort(pfannkuchen))
File "D:\programming\Aufgabe3a.py", line 47, in best_sort
flipi.append(best_sort(sort_lis))
File "D:\programming\Aufgabe3a.py", line 53, in best_sort
return flips[ret]
IndexError: list index out of range
英文:
I wrote this algorithm that returns the shortest sequence using operations to sort a list of numbers.
is sorted by always reversing the order of the first n elements of the list and removing the first element from the list.
def wenden(arr, n):
#dreht die Ersten n elemente um
i = 0
while i < n // 2:
arr[i], arr[n - i-1] = arr[n - i-1], arr[i]
i += 1
arr.pop(0)
return arr
def ist_Sortirt(arr):
for i in range(len(arr) - 1):
if arr[i] > arr[i+1]:
return False
return True
def best_sort(arr):
flips = []
if not ist_Sortirt(arr):
for i in range(1, len(arr)-1):
sort_lis = wenden(arr, i + 1)
flipi = [i+1]
flipi.append(best_sort(sort_lis))
flips.append(flipi)
ret = 0
for i in range(1, len(flips)-1):
if len(flips[i]) < len(flips[ret]):
ret = i
return flips[ret]
else:
return []
I have tested the script with different input lists, a list with the phrases should be output, but I always get a list index out of range error at return flips[ret]
in best_sort()
if the input is longer than 3 numbers
input: [2,1,3,6]
Traceback (most recent call last):
File "D:\programming\Aufgabe3a.py", line 69, in <module>
print(best_sort(pfannkuchen))
File "D:\programming\Aufgabe3a.py", line 47, in best_sort
flipi.append(best_sort(sort_lis))
File "D:\programming\Aufgabe3a.py", line 53, in best_sort
return flips[ret]
IndexError: list index out of range
答案1
得分: 0
I fixed the issue.
Try it now.
Change the loop range in best_sort instead of looping from 1 to len(arr) - 1, you should loop from 0 to len(arr) to avoid index out of range errors.
我已修复问题。
现在试试。
在best_sort中更改循环范围,不要从1到len(arr) - 1循环,应该从0到len(arr)循环,以避免索引超出范围的错误。
英文:
I fixed the issue.
Try it now.
Change the loop range in best_sort instead of looping from 1 to len(arr) - 1, you should loop from 0 to len(arr) to avoid index out of range errors.
def wenden(arr, n):
i = 0
while i < n // 2:
arr[i], arr[n - i - 1] = arr[n - i - 1], arr[i]
i += 1
arr.pop(0)
return arr
def ist_Sortirt(arr):
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
return False
return True
def best_sort(arr):
if len(arr) <= 1:
return []
flips = []
if not ist_Sortirt(arr):
for i in range(len(arr)):
sort_lis = copy.deepcopy(arr)
sort_lis = wenden(sort_lis, i + 1)
flipi = [i + 1]
flipi.append(best_sort(sort_lis))
flips.append(flipi)
ret = 0
for i in range(1, len(flips)):
if len(flips[i]) < len(flips[ret]):
ret = i
return flips[ret]
else:
return []
答案2
得分: 0
def best_sort(arr):
flips = []
if not is_sorted(arr):
for i in range(1, len(arr)-1):
sort_list = reverse(arr, i + 1)
flip_i = [i+1]
flip_i.append(best_sort(sort_list))
flips.append(flip_i)
ret = 0
for i in range(1, len(flips)-1):
if len(flips[i]) < len(flips[ret]):
ret = i
return flips[ret]
Eventually, the code reaches a point where arr
only has two elements.
So the loop for i in range(1, len(arr)-1)
becomes for i in range(1, 1)
and
does not execute at all, therefore no items are appended to flips
, and therefore flips[ret]
is out of range.
英文:
def best_sort(arr):
flips = []
if not ist_Sortirt(arr):
for i in range(1, len(arr)-1):
sort_lis = wenden(arr, i + 1)
flipi = [i+1]
flipi.append(best_sort(sort_lis))
flips.append(flipi)
ret = 0
for i in range(1, len(flips)-1):
if len(flips[i]) < len(flips[ret]):
ret = i
return flips[ret]
Eventually, the code reaches a point where arr
only has two elements.
So the loop for i in range(1, len(arr)-1)
becomes for i in range(1, 1)
and
does not execute at all, therefore no items are appended to flips
, and therefore flips[ret]
is out of range.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论