1

I am getting an indexerror: list index out of range when I try to run this program with any sort of list.
Any ideas?

def binaryListSort(aList):
    '''takes a list of binary numbers and puts them in ascending order
    inputs: a list of binary integers, aList Outputs: a new list with numbers
    in ascending order.'''

    if aList[0] == 0:
        return aList[0] + binaryListSort(aList[1:])
    else:
        return binaryListSort(aList[1:]) + aList[0]
2
  • Which programming language are you talking about? Commented Feb 16, 2014 at 14:53
  • python sorry thanks for looking! Commented Feb 16, 2014 at 14:55

3 Answers 3

2

This doesn't seem to be doing any kind of sorting at all. All you're doing is checking that the first element of the list is 0, then chopping off the first and checking again. If it's never 0, eventually it will try to check the first element of an empty list, hence the error.

Sign up to request clarification or add additional context in comments.

1 Comment

I think it's trying to sort. It took me a while to think of what "binary integers" could be, but if the only entries of the list were 0 and 1 it should work.. if the OP handles the empty list case, and the OP fixes the code so it doesn't try to add an integer to a list.
1

In every recursive, you will decrease the list length by 1, so you will get a list empty which length is just 1, so when you use the aList[1:], it will throw the indexerror. and you should consider the aList is empty list.

you can give test with an example, and you can use the following code, it will work well.

def binaryListSort(aList):
    '''takes a list of binary numbers and puts them in ascending order
    inputs: a list of binary integers, aList Outputs: a new list with numbers
    in ascending order.'''

    if len(aList) == 0:
      return

    if len(aList) == 1:
      return aList

    if aList[0] == 0:
      return [aList[0]] + binaryListSort(aList[1:])
    else:
      return binaryListSort(aList[1:]) + [aList[0]]

2 Comments

This code won't work, because you can't add an integer to a list.
yes, you're right,I did not notice that.I have fixed this now.
-1

With this loop, you'll finally call binaryListSort with a list containing just 1 item. Then is aList[1] not defined and thus aList[1:] will cause an indexerror You need to insert code into your white line that will return aList if the length of aList is one.

2 Comments

Slicing never gives an index error. If the list is less than one element long, slicing [1:] simply gives an empty list. The index error comes when the code subsequently accesses element 0.
My bad indeed. However, the problem will be solved by adding the code if len(aList) == 1: return aList

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.