Python/sage: can lists start at index 1?

后端 未结 5 1111
逝去的感伤
逝去的感伤 2021-01-04 13:25

I\'ve downloaded from a supposedly serious source a sage script. It doesn\'t work on my computer, and a quick debugging showed that a problem came from the fact that at some

5条回答
  •  时光取名叫无心
    2021-01-04 14:09

    Well I too was facing the same idea on how to implement the method of indexing to be start from 1. I wanted to implement the Insertion Sort Algorithm which is as follows: Insertion Sort Algorithm

    As we already know python list start from 0, what I did was following:

    A = ['dummy',5,2,6,4,1,3]
    for j in range(2,len(A)):
        key = A[j]
        i=j-1
        while i>0 and A[i]>key:
            A[i+1] = A[i]
            i = i-1
        A[i+1] = key
    A.pop(0)
    print A
    

    I Just added a 'Dummy' in index 0, did all the work like in Algorithm and removed the 'dummy' again. This was just a cheating method.

提交回复
热议问题