diff options
Diffstat (limited to 'day2.5')
| -rw-r--r-- | day2.5/task1/task1.py | 45 | ||||
| -rw-r--r-- | day2.5/task2/task2.py | 54 |
2 files changed, 99 insertions, 0 deletions
diff --git a/day2.5/task1/task1.py b/day2.5/task1/task1.py new file mode 100644 index 0000000..f7ce690 --- /dev/null +++ b/day2.5/task1/task1.py @@ -0,0 +1,45 @@ +n = int(input()) +RIGHT, DOWN, LEFT, UP = range(4) + +matrix = [[0 for _ in range(n)] for _ in range(n)] + +current = [0, 0] +direction = RIGHT + +for i in range(1, n ** 2 + 1): + matrix[current[1]][current[0]] = i + + if direction == RIGHT: + if current[0] + 1 >= n or matrix[current[1]][current[0] + 1] != 0: + direction = DOWN + current[1] += 1 + else: + current[0] += 1 + + elif direction == DOWN: + if current[1] + 1 >= n or matrix[current[1] + 1][current[0]] != 0: + direction = LEFT + current[0] -= 1 + else: + current[1] += 1 + + elif direction == LEFT: + if current[0] - 1 < 0 or matrix[current[1]][current[0] - 1] != 0: + direction = UP + current[1] -= 1 + else: + current[0] -= 1 + + elif direction == UP: + if current[1] - 1 < 0 or matrix[current[1] - 1][current[0]] != 0: + direction = RIGHT + current[0] += 1 + else: + current[1] -= 1 + + +# Переводим все числа в строки и делаем их необходимой длины, чтобы вывод был наглядным +# Длина, до которой дополняем все числа, точно будет равна длине самого большого числа в матрице +maxlen = len(str(n ** 2)) +for row in matrix: + print(*map(lambda num: str(num).rjust(maxlen, ' '), row)) diff --git a/day2.5/task2/task2.py b/day2.5/task2/task2.py new file mode 100644 index 0000000..dc89b3c --- /dev/null +++ b/day2.5/task2/task2.py @@ -0,0 +1,54 @@ +from math import ceil + +n = int(input('Введите размер стороны матрицы: ')) +m = int(input('Введите вычисляемую строку: ')) +while m < 1 or m > n: + print('Неверный номер строки') + m = int(input('Введите вычисляемую строку: ')) + +RIGHT, DOWN, LEFT, UP = range(4) + +row = [0 for _ in range(n)] + +current = [0, 0] +direction = RIGHT +xshift = 0 +yshift = 0 + +for i in range(1, n ** 2 + 1): + if current[1] == m - 1: + row[current[0]] = i + + if direction == RIGHT: + if current[0] + 1 >= n - ceil(xshift / 2): + direction = DOWN + current[1] += 1 + xshift += 1 + else: + current[0] += 1 + + elif direction == DOWN: + if current[1] + 1 >= n - yshift // 2: + direction = LEFT + current[0] -= 1 + yshift += 1 + else: + current[1] += 1 + + elif direction == LEFT: + if current[0] - 1 < 0 + xshift // 2: + direction = UP + current[1] -= 1 + xshift += 1 + else: + current[0] -= 1 + + elif direction == UP: + if current[1] - 1 < 0 + ceil(yshift / 2): + direction = RIGHT + current[0] += 1 + yshift += 1 + else: + current[1] -= 1 + +print(*row) |