Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3월11일 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

3월11일 #3

wants to merge 1 commit into from

Conversation

jhyii
Copy link
Collaborator

@jhyii jhyii commented Mar 16, 2022

내용 & 질문
18115번은 로컬에서 돌려보면 돌아가는데 백준에서 런타임에러가 납니다!

2108번도 로컬에서는 맞게 나오는 것 같은데 백준에서는 틀렸다고 나오네요ㅜㅜ

또, 2841번은 조건문 안에서 반복문 쓰는 것 말고 더 좋은 방법이 있을 것 같은데 궁금합니다!!

<기존 제출>
1213, 2108, 18115, 2841, 2504

<추가 제출>

Copy link

@ZERO-black ZERO-black left a comment

Choose a reason for hiding this comment

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

문제 푸느라 수고하셨습니다❤ 남겨드린 피드백 참고하셔서, 코드 수정해보시고 저 다시 리뷰어로 호출해주세요!

18115: 입력 조건을 다시 한번 읽어보세요!
2108: python의 round함수가 어떻게 동작하는지 찾아보세요.
2841: 이 문제에 어떤 자료구조를 사용하면 좋을지 고민해보세요!

Comment on lines +25 to +29
for i in range(len(word)) : #나머지는 모두 짝수개이고 앞뒤에 차례로 붙음
if i%2 == 0:
deq.append(word[i])
else:
deq.appendleft(word[i])

Choose a reason for hiding this comment

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

p2. 이 방법이 틀린 방법은 아니지만, Counter를 이용해서 알파벳 별 개수를 구해주셨는데 이걸 좀 더 이용해서 코드를 짤 수도 있을거 같아요.

Comment on lines +11 to +18
for i in dict.items():
if odd_count > 1: #홀수개인 알파벳이 한개 이상이면 실패
break

if i[1]%2 !=0: #알파벳이 홀수개일 때 한개 빼놓기
odd_count += 1
odd = i[0]
word.remove(i[0])

Choose a reason for hiding this comment

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

p2. 첫번째 if 문은 사실 매 반복마다 확인할 필요는 없어요. 필요할 때만 확인하도록 고치는게 더 깔끔해 보일거 같아요😊

else:
deq.appendleft(word[i])

result = ''.join(list(deq))

Choose a reason for hiding this comment

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

join() 메소드 사용 좋아요👍

Comment on lines +5 to +6
a, b, c, d, e = map(int, input().split())
tech = [a, b, c, d, e]

Choose a reason for hiding this comment

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

p1. 일단 이 부분은, 굳이 a,b,c,d,e 로 변수를 줄 필요가 없이 한 줄에 해결할 수 있을거 같아요!!
그런데, 고치기 전에 입력 조건을 다시 한번 읽어보세요!!

Comment on lines +9 to +18
for i in tech:
if i == 1:
new_cards.append(cards[0])
cards.remove(cards[0])
elif i == 2:
new_cards.append(cards[1])
cards.remove(cards[1])
elif i == 3:
new_cards.append(cards[-1])
cards.remove(cards[-1])

Choose a reason for hiding this comment

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

p1. remove() 메소드는 현재 원소를 삭제한 후, 해당 원소 뒤에 있는 요소들은 전부 한칸씩 당겨오는 작업을 하기 때문에 시간 복잡도가 O(n)이 됩니다. 현재 n개의 명령어에 대해 remove를 수행중이니, 시간복잡도가 O(n^2)으로 큰 상황입니다. remove보다, 강의 때 배운 자료구조(컨테이너)들의 특징을 살려서 문제에 적용해보면 좋을거 같아요.

print(round(sum(num_list)/n)) #평균
print(num_list[round(n/2)]) #중앙값
print(mode) #최빈값
print(max(num_list)-min(num_list)) #범위

Choose a reason for hiding this comment

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

p2. max와 min은 리스트를 한번씩 탐색해야 하므로 O(n) 시간복잡도를 가져요. 현재 리스트 상태를 보면, 굳이 새로 탐색을 하지 않아도 될거 같아요.


n, p = map(int, input().split())

dict = {1:[0], 2:[0], 3:[0], 4:[0], 5:[0], 6:[0]} # 각 줄별로 리스트가 있는 딕셔너리

Choose a reason for hiding this comment

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

p3. 각 줄별로 리스트르르 저장하기 위해, 딕셔너리를 꼭 사용해야 할까요?
p1. dict() 기본 딕셔너리를 생성하는 함수 이름이자, 기본 클래스 이름이에요. 변수 이름을 절대 클래스 이름이나 함수 이름으로 설정하시면 안돼요. 잠재적인 버그 가능성이 있습니다.

line, pr = map(int, input().split())

# 이 음을 짚고있고 더 높은 음도 짚고있는 경우
if pr in dict[line] and max(dict[line]) > pr:

Choose a reason for hiding this comment

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

p1. dict[line]은 리스트이죠. 리스트에서 in 키워드로 탐색을 하면 처음부터 끝까지 찾아보기 때문에 O(n) 시간복잡도를 갖습니다. max() 함수도 마찬가지이구요. remove() 메소드도 O(n) 시간복잡도를 갖습니다. 문제를 잘 생각해보면, 기타 프렛을 하나씩 짚어가다가 뒤에 짚은 높은 프렛부터 손을 떼게 될거 같아요. 이런 문제 상황에 맞는 자료구조가 무엇일지 조금 더 고민해보세요!

Comment on lines +3 to +18
false = 0 #틀린 괄호열

# 괄호열의 길이가 홀수이거나, 괄호열 시작이 닫는 괄호 혹은 괄호열 끝이 여는 괄호
if len(list)%2 != 0 or list[0]== ')' or list[0]==']' or list[-1]=='(' or list[-1]=='[':
false += 1

# 여는 괄호의 개수와 닫는 괄호의 개수가 다를 때
if list.count('(') != list.count(')') or list.count('[') != list.count(']'):
false += 1

# (])[ 같은 경우
for i in range(len(list)-1):
if list[i] == '(' and list[i+1]==']':
false += 1
elif list[i] == '[' and list[i+1]==')':
false += 1

Choose a reason for hiding this comment

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

p2. 우선 전반적으로, 괄호가 유효한지부터 체크를 하고 있는데, 유효하지 않은 경우에도 나머지 연산을 계속 수행하고 있어요. 이럴 경우에는 함수화를 시켜서 유효하지 않은 경우 바로 return을 하는 방법을 이용해서 코드를 깔끔하게 짤 수 있습니다.

지금 방식은 올바르지 않은 괄호열인지 패턴을 하나씩 체크 하는 방식인데, 어떤 자료구조의 특성을 활용해서 한번의 탐색만으로 올바른 문자열인지 검증을 할 수 있어요!

힌트) 라이브코딩 - 균형잡힌 세상

Comment on lines +25 to +55
if list[i] == '(' and list[i+1]=='(':
cul.append('2*(')
elif list[i] == '(' and list[i+1]=='[':
cul.append('2*(')
elif list[i] == '[' and list[i+1]=='(':
cul.append('3*(')
elif list[i] == '[' and list[i+1]=='[':
cul.append('3*(')
# 같은 괄호가 바로 닫히면 그냥 숫자
elif list[i] == '(' and list[i+1]==')':
cul.append('(2')
elif list[i] == '[' and list[i+1]==']':
cul.append('(3')
# 닫는 괄호가 연속으로 나오면 괄호닫기
elif list[i] == ')' and list[i+1]==')':
cul.append(')')
elif list[i] == ')' and list[i+1]==']':
cul.append(')')
elif list[i] == ']' and list[i+1]==')':
cul.append(')')
elif list[i] == ']' and list[i+1]==']':
cul.append(')')
# 닫는 괄호 다음에 바로 여는 괄호가 나오면 더해줌
elif list[i] == ')' and list[i+1]=='(':
cul.append(')+')
elif list[i] == ')' and list[i+1]=='[':
cul.append(')+')
elif list[i] == ']' and list[i+1]=='(':
cul.append(')+')
elif list[i] == ']' and list[i+1]=='[':
cul.append(')+')

Choose a reason for hiding this comment

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

p2. 지금 조건문이 너무 많아요. 4*4 모든 경우에 대해 하나씩 다 코딩해주셨는데, 나올 수 있는 상황에 대해 조금 정리를 하면 좋을거 같아요. 수행하는 결과가 같은 경우들에 대해서는 조건문을 합칠 수 있을거 같아요. 추가로, 라이브코딩-균형잡힌 세상 소스코드를 한번 읽어보시는걸 추천드립니다. 하드코딩을 줄일 수 있는 힌트를 얻으실 수 있을거에요!

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