Skip to content

Solve week3 problems#9

Open
UiHyeon wants to merge 4 commits intomainfrom
UiHyeon
Open

Solve week3 problems#9
UiHyeon wants to merge 4 commits intomainfrom
UiHyeon

Conversation

@UiHyeon
Copy link
Collaborator

@UiHyeon UiHyeon commented Jun 22, 2021

📘 구현

괄호회전 문제를 풀때는 스터디에 주어진 문제와 다른 비슷한 괄호문제를 풀었던 경험으로 문제를 접근했습니다. 괄호의 올바름 여부를 판별하는 것에는 stack을 사용하는 것이 편리함을 다시 알 수 있었습니다. 요즘 알고리즘 문제를 풀때 코드의 길이가 길어지거나 깊이가 깊어질때 코드 수정의 어려움을 겪었습니다. 이번 문제에서는 그 어려움을 해결하고자 일부 알고리즘 로직을 함수화 시켜 분리해 코드의 복잡함을 감소시키고 가독성을 올리는 형식으로 구현하였습니다.

행렬 테두리 회전 문제는 프로그래머스에서 코딩테스트를 응시할 당시 기출된 문제였고 풀지 못했던 문제였습니다. 이 문제는 행렬의 row와 column을 헷갈리지 않는 것이 가장 큰 중심이 되는 것 같았습니다. 그리고 문제를 풀면서 처음 구현 방식을 떠올릴 때 '주어진 순서로 행렬을 돌리면서 값을 바꿔주면 되구나'라고 생각했는데 이렇게 상 우 하 좌(시계방향)으로 이동을 하게 되면 원하는 대로 숫자의 이동이 이뤄질 수가 없는 구조였습니다. 숫자는 시계방향으로 이동하지막 이 움직임의 알고리즘 로직은 반시계방향으로 설계를 하고 구현을 해야 했습니다. 이 내용을 이해하기까지 오래 걸렸고 어려웠습니다. 다음에 비슷한 유형이 나온다면 더 수월하게 풀 수 있을것 같습니다.

💬 후기

괄호회전에선 어떻게 해야 이 괄호들의 나열이 올바른 괄호의 나열인지를 판별할 수 있는지에 대한 방법을 고민했습니다. 괄호가 열리는 문자 이후 해당하는 짝의 괄호 닫힘 문자가 오지 않으면 올바르지 않는 형식으로 판별을 할 수 있었습니다.

행렬 테두리 회전 문제는 위에 문제 구현 설명에서와 같이 숫자의 이동은 시계 , 이동을 위한 움직임 알고리즘 로직은 반시계 처럼 생각을 해야함이 어려웠습니다.

@auto-assign auto-assign bot requested a review from Do-ho June 22, 2021 07:30
Copy link
Member

@Do-ho Do-ho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

더 좋은 코드가 되어가고 있군요 ㅎㅎㅎ 좋습니다 :)

Comment on lines 3 to 12
graph = []
num = 1
#행렬 생성
for i in range(rows):
temp = []
for j in range(columns):
temp.append(num)
num += 1

graph.append(temp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만든다면 한줄로도 만들 수 있지 않을까요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 짧아보이는 효과를 얻을 수 있겠네요

Comment on lines 17 to 18
slave = graph[base_r-1][end_c-1]
mini = slave
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slave는 무슨 의미를 가지고 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이건 숫자를 이동하다 보면 처음 이동시킨 숫자가 회전문에서 제외되는데 이 숫자를 마지막이동 자리에 다시 끼워넣어줘야합니다 . 그래서 slave라는 변수로 값을 저장했습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수명이 처음 보는 사람이 알아보기 어려워요 ㅠㅠ 더 가독성 있는 변수명을 써보자요~~!

Copy link
Collaborator Author

@UiHyeon UiHyeon Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아ㅋㅋㅋ 맞습니다 ㅋㅋㅋ 개그 소재로 사용했는데 변경할게요 XD

@@ -0,0 +1,36 @@
from collections import deque

def right(deq):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right도 좋지만 string이 괄호 검사로 부터 valid한지를 확인하는 것이기 때문에 isValidBracket과 같이 is를 앞에 붙여주면 좋을 것 같네요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 함수 명명법 배워갑니다!

Comment on lines 6 to 15
if check == '(' or check == '{' or check == '[':
stack.append(check)
if stack:
if check == ')' and stack.pop() != '(':
return False
if check == '}' and stack.pop() != '{':
return False
if check == ']' and stack.pop() != '[':
return False
else: return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 정규표현식으로도 한번 짜보기를 추천드립니다 ㅎㅎ 물론 짜는 속도는 if 문을 쓰는게 빠르겠죠?

@github-actions github-actions bot requested a review from Do-ho June 23, 2021 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments