-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uC2A4\uD0DD,\uD050,\uB371"
3월11일 #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from collections import Counter | ||
from collections import deque | ||
|
||
deq = deque() | ||
|
||
word = sorted(input(), reverse = True) | ||
dict = Counter(word) #각 알파벳의 수를 셈 | ||
odd_count = 0 #홀수개인 알파벳의 수 | ||
odd = '' | ||
|
||
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]) | ||
|
||
if odd_count > 1: | ||
print("I'm Sorry Hansoo") | ||
else: | ||
deq.append(odd) #홀수개인 알파벳은 한개가 중간으로 들어감 | ||
|
||
for i in range(len(word)) : #나머지는 모두 짝수개이고 앞뒤에 차례로 붙음 | ||
if i%2 == 0: | ||
deq.append(word[i]) | ||
else: | ||
deq.appendleft(word[i]) | ||
Comment on lines
+25
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 이 방법이 틀린 방법은 아니지만, Counter를 이용해서 알파벳 별 개수를 구해주셨는데 이걸 좀 더 이용해서 코드를 짤 수도 있을거 같아요. |
||
|
||
result = ''.join(list(deq)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. join() 메소드 사용 좋아요👍 |
||
print(result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
n = int(input()) | ||
cards = [i+1 for i in range(n)] | ||
new_cards = [] | ||
|
||
a, b, c, d, e = map(int, input().split()) | ||
tech = [a, b, c, d, e] | ||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. 일단 이 부분은, 굳이 a,b,c,d,e 로 변수를 줄 필요가 없이 한 줄에 해결할 수 있을거 같아요!! |
||
|
||
# 기술에 따라 new_cards에 카드 배치 | ||
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]) | ||
Comment on lines
+9
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. remove() 메소드는 현재 원소를 삭제한 후, 해당 원소 뒤에 있는 요소들은 전부 한칸씩 당겨오는 작업을 하기 때문에 시간 복잡도가 O(n)이 됩니다. 현재 n개의 명령어에 대해 remove를 수행중이니, 시간복잡도가 O(n^2)으로 큰 상황입니다. remove보다, 강의 때 배운 자료구조(컨테이너)들의 특징을 살려서 문제에 적용해보면 좋을거 같아요. |
||
|
||
# new_cards는 앞쪽 인덱스부터 들어가서 역정렬 | ||
new_cards.reverse() | ||
dict = {} | ||
# new_cards에 있는 값은 처음 카드의 순서이고 실제 카드에 적혀있는 숫자 배정 | ||
for i in range(n): | ||
dict[new_cards[i]] = i+1 | ||
|
||
sorted_dict = sorted(dict.items(), key=lambda x: x[0]) # 처음 카드의 순서로 정렬 | ||
for i in sorted_dict: | ||
print(i[1], end=' ') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from collections import Counter | ||
|
||
n = int(input()) | ||
|
||
num_list = [] | ||
for i in range(n): | ||
num_list.append(int(input())) | ||
|
||
num_list.sort() # 중앙값때문에 정렬 | ||
|
||
count = Counter(num_list) | ||
mode_list = [] | ||
|
||
for i in count.items(): | ||
if i[1] == max(count.values()): # 최빈값인 것들 모으기, 여러 개면 두 번째로 작은 거 출력해야 함 | ||
mode_list.append(i) | ||
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3. 지금 코드도 좋지만, Counter.most_common() 메소드에 대해서 한번 알아보세요! |
||
|
||
mode = int() | ||
if len(mode_list) == 1: # 최빈값인 것이 한 개일 때 | ||
mode = mode_list[0][0] | ||
else: # 최빈값인 것이 여러 개일 때 | ||
mode_list.sort() | ||
mode = mode_list[1][0] | ||
Comment on lines
+19
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외 상황 처리 잘 해주셨어요! 좋습니다👍 |
||
|
||
print(round(sum(num_list)/n)) #평균 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. 리드미 힌트에도 나와있는데, python의 round함수는 우리가 아는 것과는 조금 다르게 작동해요...! 사사오입 방식을 택해 0.5일 때 짝수에 맞춰 반올림을 합니다. |
||
print(num_list[round(n/2)]) #중앙값 | ||
print(mode) #최빈값 | ||
print(max(num_list)-min(num_list)) #범위 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. max와 min은 리스트를 한번씩 탐색해야 하므로 O(n) 시간복잡도를 가져요. 현재 리스트 상태를 보면, 굳이 새로 탐색을 하지 않아도 될거 같아요. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
list = input() | ||
|
||
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 | ||
Comment on lines
+3
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 우선 전반적으로, 괄호가 유효한지부터 체크를 하고 있는데, 유효하지 않은 경우에도 나머지 연산을 계속 수행하고 있어요. 이럴 경우에는 함수화를 시켜서 유효하지 않은 경우 바로 return을 하는 방법을 이용해서 코드를 깔끔하게 짤 수 있습니다. 지금 방식은 올바르지 않은 괄호열인지 패턴을 하나씩 체크 하는 방식인데, 어떤 자료구조의 특성을 활용해서 한번의 탐색만으로 올바른 문자열인지 검증을 할 수 있어요! 힌트) 라이브코딩 - 균형잡힌 세상 |
||
|
||
|
||
cul = [] #계산결과 | ||
|
||
for i in range(len(list)-1): | ||
# 여는 괄호가 연속으로 나오면 곱해줌 | ||
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(')+') | ||
Comment on lines
+25
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 지금 조건문이 너무 많아요. 4*4 모든 경우에 대해 하나씩 다 코딩해주셨는데, 나올 수 있는 상황에 대해 조금 정리를 하면 좋을거 같아요. 수행하는 결과가 같은 경우들에 대해서는 조건문을 합칠 수 있을거 같아요. 추가로, 라이브코딩-균형잡힌 세상 소스코드를 한번 읽어보시는걸 추천드립니다. 하드코딩을 줄일 수 있는 힌트를 얻으실 수 있을거에요! |
||
|
||
cul.append(')') # 마지막괄호 닫기 | ||
cul_str = ''.join(cul) # 리스트를 문자열로 만들기 | ||
|
||
if false == 0: | ||
print(eval(cul_str)) # 문자열을 계산해주는 함수 | ||
else: | ||
print(0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
n, p = map(int, input().split()) | ||
|
||
dict = {1:[0], 2:[0], 3:[0], 4:[0], 5:[0], 6:[0]} # 각 줄별로 리스트가 있는 딕셔너리 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3. 각 줄별로 리스트르르 저장하기 위해, 딕셔너리를 꼭 사용해야 할까요? |
||
count = 0 | ||
|
||
|
||
for i in range(n): | ||
line, pr = map(int, input().split()) | ||
|
||
# 이 음을 짚고있고 더 높은 음도 짚고있는 경우 | ||
if pr in dict[line] and max(dict[line]) > pr: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. dict[line]은 리스트이죠. 리스트에서 in 키워드로 탐색을 하면 처음부터 끝까지 찾아보기 때문에 O(n) 시간복잡도를 갖습니다. max() 함수도 마찬가지이구요. remove() 메소드도 O(n) 시간복잡도를 갖습니다. 문제를 잘 생각해보면, 기타 프렛을 하나씩 짚어가다가 뒤에 짚은 높은 프렛부터 손을 떼게 될거 같아요. 이런 문제 상황에 맞는 자료구조가 무엇일지 조금 더 고민해보세요! |
||
while(max(dict[line]) > pr): | ||
count += 1 | ||
dict[line].remove(max(dict[line])) | ||
# 이 음을 짚고있고 이 음이 가장 높은 음인 경우 | ||
elif pr in dict[line] and max(dict[line]) == pr: | ||
count += 0 | ||
# 이 음을 짚지 않고 더 높은 음을 짚고있는 경우 | ||
elif pr not in dict[line] and max(dict[line]) > pr: | ||
while(max(dict[line]) > pr): | ||
count += 1 | ||
dict[line].remove(max(dict[line])) | ||
|
||
dict[line].append(pr) | ||
count += 1 | ||
# 이 음을 짚지 않고 더 높은 음은 없는 경우 | ||
elif pr not in dict[line] and max(dict[line]) < pr: | ||
count += 1 | ||
dict[line].append(pr) | ||
|
||
|
||
print(count) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2. 첫번째 if 문은 사실 매 반복마다 확인할 필요는 없어요. 필요할 때만 확인하도록 고치는게 더 깔끔해 보일거 같아요😊