728x90
반응형
1. 정수를 나선형으로 배치하기
- 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/181832
- 구현 코드
def solution(n):
arr = [[0] * n for i in range(n)]
cnt = 1
start_row = 0
end_row = n - 1
start_col = 0
end_col = n - 1
while cnt <= n * n:
for i in range(start_col, end_col + 1):
arr[start_row][i] = cnt
cnt += 1
start_row += 1
for i in range(start_row, end_col + 1):
arr[i][end_col] = cnt
cnt += 1
end_col -= 1
for i in range(end_col, start_col - 1, -1):
arr[end_row][i] = cnt
cnt += 1
end_row -= 1
for i in range(end_row, start_row - 1, -1):
arr[i][start_col] = cnt
cnt += 1
start_col += 1
return arr
2. 특별한 이차원 배열 2
- 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/181831
- 구현 코드
def solution(arr):
answer = 1
for i in range(len(arr)):
for j in range(len(arr[i])):
if arr[i][j] != arr[j][i]:
answer = 0
return answer
3. 정사각형으로 만들기
- 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/181830
- 구현 코드
def solution(arr):
answer = []
row = len(arr)
col = len(arr[0])
if row > col:
for i in arr:
answer.append(i + [0] * (row - col))
elif row < col:
for _ in range(col - row):
arr.append([0] * col)
answer = arr
else:
answer = arr
return answer
4. 이차원 배열 대각선 순회하기
- 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/181829
- 구현 코드
def solution(board, k):
answer = 0
for i in range(min(k + 1, len(board))):
for j in range(min(k + 1 - i, len(board[i]))):
answer += board[i][j]
return answer
728x90
반응형