0

I'm not really sure on how to go about sorting a list based on offset and direction. I was writing out some code, but I feel like there's most likely a better way to go about it. I've looked through the docs, but I really haven't found anything that is applicable or it may just be out of my depth of understanding.

Say I have a list of 5 numbers: [1,2,3,4,5] and I need to sort it either left or right and by a predetermined number. For example, if I have an offset of 1 and a direction of 'R', the list that should be returned is [5,1,2,3,4]. Or I have an offset of 2 and a direction of 'L', the returning list should be [3,4,5,1,2].

My problem is that I'm running into an index out of range error at every thought that I've had. I don't really understand why. Just running into an example above with offset of 1 and direction of r:

def rotate(listOfInt, offSet, direction):
    newList = []
    if z == "r" or z == "R":
        if y == 1:
            for a in range(len(x)):
                if a == 0:
                    newList[a] = x[4]
                elif a == 4:
                    newList[a] = x[3]
                else:
                    newList[a] = x[a-1]
            return newList

With that example, I'm running into "List assignment index out of range" at this line: newList[a] = x[4]

I've tried just manually writing out the variables and assigning them the values, but again, same issue. Not exactly sure what I'm doing wrong.

Barmar
  • 741,623
  • 53
  • 500
  • 612
J. Doe
  • 11
  • 3

0 Answers0