Skip to content

Commit 2e77fe7

Browse files
authored
거의 봤어요
1 parent 4ec7173 commit 2e77fe7

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

week14/JeongMin/순위검색.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
###defaultdict 사용
3+
##장 : 딕셔너리에 키 값이 없어도 바로 사용 가능하다
4+
5+
from collections import defaultdict
6+
7+
#int defaultdict를 만들 때
8+
int_dict=defaultdict(int)
9+
int_dict['key1']
10+
int_dict['key2']=1
11+
>>defaultdict(<class 'int'>, {'key1':0, 'key2':1})
12+
13+
#list difaultdict를 만들 때
14+
list_dict=defaultdict(list)
15+
list_dict['key1']
16+
list_dict['key2']='test'
17+
list_dict >> defaultdict(<class 'list'>, {'key1':[], 'key2':'test'})
18+
'''
19+
20+
#내가 들어갈 수 있는 부분을 모두 수집한 후
21+
#key_value : 포함되는 학생의 점수
22+
#거기서 명 수 구함
23+
24+
from collections import defaultdict
25+
from itertools import combinations
26+
27+
def solution(info, query):
28+
dict = defaultdict(list)
29+
for i in info:
30+
i = i.split()
31+
dict_key = i[:-1]
32+
dict_val = int(i[-1])
33+
for size in range(0, 5):
34+
for c in combinations(dict_key, size):
35+
tmp_key = ''.join(c)
36+
dict[tmp_key].append(dict_val)
37+
38+
for key in dict.keys():
39+
dict[key].sort()
40+
41+
answer = []
42+
for q in query:
43+
q = q.replace('and ', '').replace('- ', '').split()
44+
st = ''.join(q[:-1])
45+
score = int(q[-1])
46+
47+
if st not in dict:
48+
answer.append(0)
49+
continue
50+
51+
scores = dict[st]
52+
left = 0
53+
right = len(scores)
54+
while left < right: # 이상이므로
55+
mid = (left + right) // 2
56+
if scores[mid] >= score:
57+
right = mid
58+
else:
59+
left = mid + 1
60+
answer.append(len(scores) - left)
61+
return answer
62+
63+

0 commit comments

Comments
 (0)