-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/KNU-HAEDAL/2024-SS-small-gr…
- Loading branch information
Showing
9 changed files
with
296 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
order은 손님들이 주문하던 단품메뉴들 | ||
최소 2명 이상의 손님으로부터 주문된 단품 메뉴 조합에 대해서만 코스요리 메뉴 후보에 포함 | ||
course는 추가하고 싶은 단품 메뉴 | ||
""" | ||
from itertools import combinations | ||
from collections import Counter | ||
|
||
def solution(orders, course): | ||
result = [] | ||
|
||
for c in course: | ||
comb_list = [] | ||
|
||
for order in orders: | ||
# 각 주문 알파벳 순으로 정렬 + combinations를 사용해서 주문된 단품 메뉴 조합 생성 | ||
comb_list += combinations(sorted(order), c) | ||
|
||
# 생성된 조합들 Counter 사용해서 셈 -> 각 조합 몇 번 주문되었는지 check | ||
most_common_comb = Counter(comb_list).most_common() | ||
|
||
# 2명 이상 주문한 조합 중 가장 많이 주문한 조합 result에 추가 | ||
result += [comb for comb, count in most_common_comb if count > 1 and count == most_common_comb[0][1]] | ||
|
||
return [''.join(comb) for comb in sorted(result)] | ||
|
||
orders1 = ["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"] | ||
course1 = [2, 3, 4] | ||
# result1 = ["AC", "ACDE", "BCFG", "CDE"] | ||
|
||
orders2 = ["ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD"] | ||
course2 = [2, 3, 5] | ||
# result2 = ["ACD", "AD", "ADE", "CD", "XYZ"] | ||
|
||
orders3 = ["XYZ", "XWY", "WXA"] | ||
course3 = [2, 3, 4] | ||
# result3 = ["WX", "XY"] | ||
|
||
print(solution(orders1, course1)) | ||
print(solution(orders2, course2)) | ||
print(solution(orders3, course3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
이진 트리를 표현한 리스트 nodes를 인자로 받음 | ||
해당 이진 트리에 대하여 전위 순회, 중위 순회, 후위 순회 결과를 반환하는 solution() 함수를 구하시오 | ||
""" | ||
def preorder(nodes, idx): # 전위 순회 | ||
if idx >= len(nodes): | ||
return [] | ||
return [nodes[idx]] + preorder(nodes, 2 * idx + 1) + preorder(nodes, 2 * idx + 2) | ||
|
||
def inorder(nodes, idx): # 중위 순회 | ||
if idx >= len(nodes): | ||
return [] | ||
return inorder(nodes, 2 * idx + 1) + [nodes[idx]] + inorder(nodes, 2 * idx + 2) | ||
|
||
def postorder(nodes, idx): | ||
if idx >= len(nodes): | ||
return [] | ||
return postorder(nodes, 2 * idx + 1) + postorder(nodes, 2 * idx + 2) + [nodes[idx]] | ||
|
||
def solution(nodes): | ||
pre_order = preorder(nodes, 0) | ||
in_order = inorder(nodes, 0) | ||
post_order = postorder(nodes, 0) | ||
return [" ".join(map(str, pre_order)), " ".join(map(str, in_order)), " ".join(map(str, post_order))] | ||
|
||
nodes = [1, 2, 3, 4, 5, 6, 7] | ||
# return = ["1 2 4 5 3 6 7", "4 2 5 1 6 3 7", "4 5 2 6 7 3 1"] | ||
|
||
result = solution(nodes) | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
첫 번째 인수 lst를 이용해서 이진 탐색 트리를 생성하고 | ||
두 번째 인수 search_lst에 있는 각 노드를 이진 탐색 트리에서 찾을 수 있는지 확인하여 | ||
True 또는 False를 담은 리스트 result를 반환하는 함수 solution()을 작성 | ||
""" | ||
|
||
class TreeNode: | ||
def __init__(self, value): | ||
self.value = value | ||
self.left = None | ||
self.right = None | ||
|
||
def insert_into_bst(root, value): | ||
if root is None: | ||
return TreeNode(value) # 트리가 비어있으면 새로운 노드 반환 | ||
if value < root.value: # 안비어있으면 왼쪽 or 오른쪽에 삽입 | ||
root.left = insert_into_bst(root.left, value) | ||
else: | ||
root.right = insert_into_bst(root.right, value) | ||
return root | ||
|
||
def search_bst(root, value): # value가 트리에 존재하는지 확인 | ||
if root is None: # 비어있으면 False | ||
return False | ||
if root.value == value: # 같으면 True | ||
return True | ||
elif value < root.value: # 작으면 왼쪽 서브트리 | ||
return search_bst(root.left, value) | ||
else: # 크면 오른쪽 서브트리 | ||
return search_bst(root.right, value) | ||
|
||
def solution(lst, search_lst): # 일단 lst를 이용해서 이진 탐색 트리 생성 | ||
if not lst: # 비어있는 경우 | ||
return [False] * len(search_lst) # search_lst 를 이용해서 True 아니면 False 반환 | ||
|
||
root = None | ||
for value in lst: | ||
root = insert_into_bst(root, value) | ||
|
||
result = [] | ||
for value in search_lst: # search_lst 각 값을 트리에서 검색하고 결과를 result 리스트에 추가 | ||
result.append(search_bst(root, value)) | ||
|
||
return result | ||
|
||
lst1 = [5, 3, 8, 4, 2, 1, 7, 10] | ||
search_lst1 = [1, 2, 5, 6] | ||
# answer1 = [True, True, True, False] | ||
|
||
lst2 = [1, 3, 5, 7, 9] | ||
search_lst2 = [2, 4, 6, 8, 10] | ||
# answer2 = [False, False, False, False, False] | ||
|
||
print(solution(lst1, search_lst1)) | ||
print(solution(lst2, search_lst2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
N명의 참가자에게 1부터 N번의 번호를 차례로 배정 | ||
1vs2 3vs4 ... N-1 vs N 해서 이기면 다음 라운드 진출 | ||
다음 라운드 진출자들 다시 번호 매김 | ||
N : 2^1 이상 2^20 이하인 자연수 (2의 지수로 주어지므로 부전승 X) | ||
A, B : N 이하인 자연수 (A != B) | ||
""" | ||
def solution(N, A, B): | ||
round_num = 1 | ||
while A != B: | ||
A = (A + 1) // 2 | ||
B = (B + 1) // 2 | ||
round_num += 1 | ||
return round_num - 1 | ||
|
||
N = 8 | ||
A = 4 | ||
B = 7 | ||
print(solution(N, A, B)) | ||
# answer = 3 | ||
# 1 -> 1, 2 -> 1, 3 -> 2, 4 -> 2 | ||
# 5 -> 3, 6 -> 3, 7 -> 4, 8 -> 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
모든 판매원은 칫솔 판매로 생기는 이익에서 10%를 계산해 자신을 조직에 참여시킨 추천인에게 배분 나머지는 자신이 가짐 | ||
모든 판매원은 자신이 조직에 추천하여 가입시킨 판매원의 이익금의 10%를 자신이 가짐 | ||
10% 계산할 때 원 단위에서 자르고, 10% 계산한 금액이 1원 미만이면 분배 X | ||
""" | ||
|
||
# 판매원 얻은 이익 계산하고 10%를 추천인에게 분배 | ||
def distribute_profit(name, profit, parent, earnings): | ||
if name == "-" or profit == 0: # 추천인 "-"이거나 분배할 이익이 1원 미만이면 종료 | ||
return | ||
parent_profit = profit // 10 | ||
earnings[name] += profit - parent_profit | ||
distribute_profit(parent[name], parent_profit, parent, earnings) | ||
|
||
|
||
def solution(enroll, referral, seller, amount): | ||
# 일단 돈 줘야 하는 사람을 key로, 받는 사람을 value로 대응시켜서 저장 | ||
parent = {enroll[i] : referral[i] for i in range(len(enroll))} # 추천인 저장 | ||
earnings = {name : 0 for name in enroll} # 각 seller 총 수익 저장하는 dictionary | ||
|
||
for i in range(len(seller)): | ||
profit = amount[i] * 100 | ||
seller_info = seller[i] | ||
distribute_profit(seller[i], profit, parent, earnings) | ||
|
||
return [earnings[name]for name in enroll] | ||
|
||
|
||
enroll = ["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] | ||
referral = ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] | ||
|
||
seller1 = ["young", "john", "tod", "emily", "mary"] | ||
amount1 = [12, 4, 2, 5, 10] | ||
|
||
seller2 = ["sam", "emily", "jaimie", "edward"] | ||
amount2 = [2, 3, 5, 4] | ||
# result1 = [360, 958, 108, 0, 450, 18, 180, 1080] | ||
|
||
print(solution(enroll, referral, seller1, amount1)) | ||
# result2 = [0, 110, 378, 180, 270, 450, 0, 0] | ||
|
||
print(solution(enroll, referral, seller2, amount2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def solution(n): | ||
def check(ls, new): | ||
for i in range(len(ls)): | ||
if new == ls[i] or (len(ls)-i) == abs(ls[i]-new): | ||
return False | ||
return True | ||
def dfs(n, ls): | ||
if len(ls) == n: | ||
return 1 | ||
cnt = 0 | ||
for i in range(n): | ||
if check(ls, i): | ||
cnt += dfs(n, ls+[i]) | ||
return cnt | ||
|
||
return dfs(n, []) | ||
|
||
n = 4 | ||
print(solution(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
def distribution(arrows, score, arrow_per_person=None, idx=0): | ||
if arrow_per_person is None: | ||
arrow_per_person = [0] * score | ||
|
||
if arrows == 0: | ||
return [tuple(arrow_per_person)] | ||
|
||
distributions = [] | ||
|
||
for i in range(idx, score): | ||
if arrows > 0: | ||
arrow_per_person[i] += 1 | ||
next_distributions = distribution(arrows - 1, score, arrow_per_person, i) | ||
distributions.extend(next_distributions) | ||
arrow_per_person[i] -= 1 | ||
|
||
return distributions | ||
|
||
def solution(n, info): | ||
answer = [] | ||
maxv = 0 | ||
real = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] | ||
all_distributions = distribution(n, 11) | ||
|
||
for temp in all_distributions: | ||
ryan = 0 | ||
muzi = 0 | ||
for i in range(len(temp)): | ||
if temp[i] or info[i]: | ||
if temp[i] > info[i]: | ||
ryan += real[i] | ||
else: | ||
muzi += real[i] | ||
|
||
gap = ryan - muzi | ||
|
||
if gap > maxv: | ||
answer = [] | ||
answer.append(temp) | ||
maxv = gap | ||
elif gap < maxv: | ||
continue | ||
else: | ||
answer.append(temp) | ||
|
||
if not answer: | ||
return [-1] | ||
elif maxv == 0: | ||
return [-1] | ||
|
||
idx = -1 | ||
for i in range(len(answer[0])-1, -1, -1): | ||
maxv = -float('inf') | ||
for a in answer: | ||
if a[i] == 0: | ||
continue | ||
if maxv < a[i]: | ||
maxv = a[i] | ||
if maxv > 0: | ||
idx = i | ||
break | ||
|
||
answer.sort(key = lambda x:-x[idx]) | ||
|
||
return answer[0] | ||
|
||
n, info = 5, [2,1,1,1,0,0,0,0,0,0,0] | ||
n, info = 1, [1,0,0,0,0,0,0,0,0,0,0] | ||
n, info = 9, [0,0,1,2,0,1,1,1,1,1,1] | ||
n, info = 10, [0,0,0,0,0,0,0,0,3,4,3] | ||
print(solution(n, info)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
193 | ||
197 |