Program - Rotate an array of n elements to the right by k steps.


Topic: Program - Rotate an array of n elements to the right by k steps.

Solution

org = [1,2,3,4,5,6,7]
result = org[:]
steps = 3
for idx,num in enumerate(org):
    if idx+steps < len(org):
        result[idx+steps] = org[idx]
    else:
        result[idx+steps-len(org)] = org[idx]
print(result)



List all Python Programs