Last active
November 16, 2018 09:22
-
-
Save dinkopehar/6df5a3dae23ee94f0536f79582f655c5 to your computer and use it in GitHub Desktop.
cyclic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_emtpy_cyclic_array(rows = 3, columns = 3): | |
matrix = [] | |
for i in range(rows): | |
row_array = [] | |
for j in range(columns): | |
row_array.append(" x ") | |
matrix.append(row_array) | |
return matrix | |
def print_cyclic_array(cyclic): | |
print() | |
for row in cyclic: | |
print(row) | |
print("-" * 30) | |
def check_if_empty_cell(symbol): | |
return True if symbol != " x " else False | |
def fill_cyclic_array(cyclic, rows, columns): | |
counter = 1 | |
length_of_cyclic_array = rows * columns | |
real_rows = rows - 1 | |
real_columns = columns - 1 | |
rows = rows - 1 | |
columns = columns - 1 | |
# Smjerovi upisivanja brojeva u matricu: | |
# 0 - lijevo | |
# 1 - gore | |
# 2 - desno | |
# 3 - dole | |
clock_direction = 0 | |
while counter <= length_of_cyclic_array: | |
if clock_direction == 0: | |
cyclic[rows][columns] = counter | |
columns -= 1 | |
if check_if_empty_cell(cyclic[rows][columns]): | |
clock_direction = 1 | |
rows -= 1 | |
columns += 1 | |
elif columns <= 0: | |
clock_direction = 1 | |
columns = 0 | |
elif clock_direction == 1: | |
cyclic[rows][columns] = counter | |
rows -= 1 | |
if check_if_empty_cell(cyclic[rows][columns]): | |
clock_direction = 2 | |
rows += 1 | |
columns += 1 | |
elif rows <= 0: | |
clock_direction = 2 | |
rows = 0 | |
elif clock_direction == 2: | |
cyclic[rows][columns] = counter | |
columns += 1 | |
if check_if_empty_cell(cyclic[rows][columns]): | |
clock_direction = 3 | |
rows += 1 | |
columns -= 1 | |
elif columns >= real_columns: | |
clock_direction = 3 | |
columns = real_columns | |
elif clock_direction == 3: | |
cyclic[rows][columns] = counter | |
rows += 1 | |
if check_if_empty_cell(cyclic[rows][columns]): | |
clock_direction = 0 | |
rows -= 1 | |
columns -= 1 | |
elif rows >= real_rows: | |
clock_direction = 0 | |
rows = real_rows | |
counter += 1; | |
if __name__ == "__main__": | |
rows, columns = 3, 7 | |
cyclic = create_emtpy_cyclic_array(rows, columns) | |
print_cyclic_array(cyclic) | |
fill_cyclic_array(cyclic, rows, columns) | |
print_cyclic_array(cyclic) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment