rotLeft function - jungling algorithm

Words
0
Reading
0 min
Listen
Play
8y
#!/bin/python3

import math
import os
import random
import re
import sys

def gcd(a, b):
    if b==0: 
        return a
    else:
        return gcd(b, a % b)

# Complete the rotLeft function below.
def rotLeft(a, d):
    #jungling algorithm - instead of moving one by one, divide the array into different sets 
    n = len(a)
    print("Great common divisior %d" % gcd(d, n))
    for i in range(gcd(d, n)):
        temp = a[i] 
        j = i 
        while 1: 
            k = j + d 
            if k >= n: 
                k = k - n 
            if k == i: 
                break
            a[j] = a[k] 
            j = k 
        a[j] = temp 
    return a



if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nd = input().split()

    n = int(nd[0])

    d = int(nd[1])

    a = list(map(int, input().rstrip().split()))

    result = rotLeft(a, d)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()
rotLeft function - jungling algorithm | Ecency