Why do i get a list index out of range error with entries with a length of four digits?

Question:

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:programmingAufgabe3a.py", line 69, in <module>
    print(best_sort(pfannkuchen))
  File "D:programmingAufgabe3a.py", line 47, in best_sort
    flipi.append(best_sort(sort_lis))
  File "D:programmingAufgabe3a.py", line 53, in best_sort
    return flips[ret]
IndexError: list index out of range
Asked By: trashy-coder

||

Answers:

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 []
Answered By: Abdulmajeed
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.

Answered By: John Gordon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.