Skip to content

Commit 3a9fe01

Browse files
Add files via upload
1 parent 9019805 commit 3a9fe01

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

Day16/DartGame.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 39. 다트 게임
2+
3+
# 내 풀이 X ... 뭔가 복잡해
4+
def solution(dartResult):
5+
answer = 0
6+
7+
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
8+
option = {'*' : 2, '#' : -1}
9+
10+
Third_period = [0, 0, 0]
11+
flag = -1
12+
13+
for idx, dart in enumerate(dartResult):
14+
if dart.isdigit():
15+
flag += 1
16+
if dart == '0':
17+
continue
18+
elif dartResult[idx + 1].isdigit(): # 10 이상일 때
19+
Third_period[flag] = int(dart) * 10
20+
flag -= 1
21+
else:
22+
Third_period[flag] = int(dart)
23+
elif dart in 'SDT': # SDT
24+
Third_period[flag] **= bonus[dart]
25+
else:
26+
if dart == '*':
27+
Third_period[flag - 1] *= 2
28+
29+
Third_period[flag] *= option[dart]
30+
31+
answer = sum(Third_period)
32+
return answer
33+
34+
dartResult_1 = '1S2D*3T'
35+
dartResult_2 = '1D2S#10S'
36+
dartResult_3 = '1D2S0T'
37+
dartResult_4 = '1S*2T*3S'
38+
dartResult_5 = '1D#2S*3S'
39+
dartResult_6 = '1T2D3D#'
40+
dartResult_7 = '1D2S3T*'
41+
42+
print(solution(dartResult_1))
43+
print(solution(dartResult_2))
44+
print(solution(dartResult_3))
45+
print(solution(dartResult_4))
46+
print(solution(dartResult_5))
47+
print(solution(dartResult_6))
48+
print(solution(dartResult_7))
49+
50+
import re
51+
52+
def solution_best(dartResult):
53+
answer = 0
54+
55+
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
56+
option = {'' : 1, '*' : 2, '#' : -1}
57+
58+
p = re.compile('(\d+)([SDT])([*#]?)')
59+
dart = p.findall(dartResult)
60+
for i in range(len(dart)):
61+
if dart[i][2] == '*' and i > 0:
62+
dart[i - 1] *= 2
63+
dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]
64+
65+
answer = sum(dart)
66+
return answer
67+
68+
print(solution_best(dartResult_1))
69+
print(solution_best(dartResult_2))
70+
print(solution_best(dartResult_3))
71+
print(solution_best(dartResult_4))
72+
print(solution_best(dartResult_5))
73+
print(solution_best(dartResult_6))
74+
print(solution_best(dartResult_7))

Day16/FailRate.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 38. 실패율
2+
3+
# 내 풀이 X ... 뭔가 어렵네...
4+
def solution(N, stages):
5+
answer = []
6+
7+
# 스테이지 도달한 플레이어 수 ... k
8+
success_players = len(stages)
9+
10+
# 실패율 리스트
11+
failing_rate_lst = []
12+
13+
for i in range(1, N + 1):
14+
# 스테이지 도달했으나 클리어하지 못한 플레이어 수
15+
failing_cnt = 0
16+
for j in range(len(stages)):
17+
if stages[j] == i:
18+
failing_cnt += 1
19+
20+
if failing_cnt == 0:
21+
failing_rate_lst.append(0)
22+
else:
23+
failing_rate_lst.append(failing_cnt / success_players)
24+
success_players = success_players - failing_cnt
25+
26+
failing_rate_reverse_sort = sorted(failing_rate_lst, reverse = True)
27+
# print(failing_rate_reverse_sort)
28+
29+
for i in range(len(failing_rate_reverse_sort)):
30+
answer.append(failing_rate_lst.index(failing_rate_reverse_sort[i]) + 1)
31+
failing_rate_lst[failing_rate_lst.index(failing_rate_reverse_sort[i])] = 2
32+
33+
return answer
34+
35+
N_1 = 5
36+
N_2 = 4
37+
38+
stages_1 = [2, 1, 2, 6, 2, 4, 3, 3]
39+
stages_2 = [4, 4, 4, 4, 4]
40+
41+
# print(solution(N_1, stages_1))
42+
# print(solution(N_2, stages_2))
43+
44+
# 이중 리스트 풀이 ... [2, 1, 2, 6, 2, 4, 3, 3] -> [[1], [2, 2, 2], [3, 3], [4], [6]]
45+
def solution_mine(N, stages):
46+
answer = []
47+
48+
49+
return answer
50+
51+
# print(solution_mine(N_1, stages_1))
52+
# print(solution_mine(N_2, stages_2))
53+
54+
def solution_best(N, stages):
55+
answer = []
56+
57+
# 실패율 딕셔너리
58+
result = {}
59+
60+
# 스테이지에 도달한 플레이어 수 = success_players / denominator : 분모
61+
denominator = len(stages)
62+
63+
for stage in range(1, N + 1):
64+
if denominator != 0:
65+
failing_count = stages.count(stage)
66+
result[stage] = failing_count / denominator
67+
denominator -= failing_count
68+
else:
69+
result[stage] = 0
70+
answer = sorted(result, key=lambda x: result[x], reverse=True)
71+
return answer
72+
73+
print(solution_best(N_1, stages_1))
74+
print(solution_best(N_2, stages_2))

0 commit comments

Comments
 (0)