Describing algorithms to implement higher level operations in terms of the lower level operations My question concerns problem (b). This is my attempt:



def shift_left(D,k)
    for j = 0 to k:
      x_current = D.delete_first() //this remove and return first item
      D.insert_last(x_current)
     
    return D




The answer key was given the following for the solution. I am just trying to figure out if my solution is correct. I know this solution key uses recursion. Does my solution work as well?
Your code solves the problem in a desired way, it is concise which is good.
Some comments
Your design limits the shifting to k &gt; len(D) - 1, if it is your intention, then it is perfect otherwise this could be solved by doing
def shift_left(D, k):
    k = k % len(D) #the modulus makes k to be in the positive integer domain
    if (k &lt; 1):
        return
    x = D.delete_first()
    D.insert_last(x)
    #it could be done with a for and saves recursively comparing k each time
    shift_left(D, k-1)

An alternative
def shift_left(D, k):
    k = k % len(D)
    if (k &lt; 1):
        return
    #avoids recursion, which saves recursive calls and passing parameters
    #(could be done using a while)
    for i in range(0,k)
        x = D.delete_first()
        D.insert_last(x)