import numpy as np
import time
import random

# simple array example
arr = [ [11,12,13,14,15], [21,22,23,24,25], [31,32,33,34,35] ]

# ----------------------------------------------------------------------------
#    P L A I N    R O T A T I O N      (create and check)
# ----------------------------------------------------------------------------
print( "Plain rotation check")

# return an array rotated left (counterclockwise) by 90 degrees
def rotPlain90( arr ):
    b = [[0]*len(arr) for i in range( len(arr[0]) ) ]
    for i in range( len(arr) ):
        for j in range( len(arr[i]) ):
            b[len(b)-j-1][i] = arr[i][j]
    return b


for row in arr: print(row)

b = rotPlain90(arr)
for row in b: print(row)

input() # for demo purposes

# ----------------------------------------------------------------------------
#    N U M P Y    R O T A T I O N      (create and check)
# ----------------------------------------------------------------------------
print( "Numpy rotation check")

nparr =  np.array( arr )
print( nparr )
nparrb = np.rot90( nparr )
print( nparrb )


input()  # for demo purposes

# ----------------------------------------------------------------------------
#    C O N T E S T    S P A C E
# ----------------------------------------------------------------------------

# Contest parameters
Height, Width = 20, 20
Nruns = 10000

# Echo the contest parameters
print( "Compare the speed of manual and numpy rotation of an array" )
print( "Height, Width, Nruns  ", Height, Width, Nruns )

# create contest data
arr = [random.choices( range(100), k = Width )  for i in range(Height) ]
# for row in arr: print(row)  # disable for big arrs

# perform plain rotation Nruns times
t1 =  time.time()
for k in range(Nruns):
    arrb = rotPlain90( arr )
t2 =  time.time()
print( 'time  plain:', t2-t1 )

# perform numpy rotation Nruns times
t3 =  time.time()
for k in range(Nruns):
    nparrb = np.rot90( nparr )
t4 =  time.time()
print( 'time  numpy:', t4-t3 )
print( 'numpy speedup factor: ', (t2-t1) / (t4-t3) )



################################################################################
# try the same with array flipping or rolling instead of rotation
# https://numpy.org/doc/stable/reference/generated/numpy.flip.html
# https://numpy.org/doc/stable/reference/generated/numpy.roll.html













