为什么我在条目长度为四位数时会收到列表索引超出范围的错误?

huangapple go评论72阅读模式
英文:

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 &lt; 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] &gt; 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]) &lt; 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 &quot;D:\programming\Aufgabe3a.py&quot;, line 69, in &lt;module&gt;
    print(best_sort(pfannkuchen))
  File &quot;D:\programming\Aufgabe3a.py&quot;, line 47, in best_sort
    flipi.append(best_sort(sort_lis))
  File &quot;D:\programming\Aufgabe3a.py&quot;, 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 &lt; 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] &gt; arr[i + 1]:
            return False
    return True

def best_sort(arr):
    if len(arr) &lt;= 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]) &lt; 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]) &lt; 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.

huangapple
  • 本文由 发表于 2023年4月4日 06:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定