Skip to content

Commit ae0ec55

Browse files
Add files via upload
1 parent cf07eb2 commit ae0ec55

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 45. 약수의 개수와 덧셈
2+
3+
def solution(left, right):
4+
answer = 0
5+
6+
for cost in range(left, right + 1):
7+
aliquot_cnt = 0
8+
for aliquot in range(1, cost + 1):
9+
if cost % aliquot == 0:
10+
aliquot_cnt += 1
11+
12+
if aliquot_cnt % 2 == 0:
13+
answer += cost
14+
else:
15+
answer -= cost
16+
return answer
17+
18+
left_1 = 13
19+
left_2 = 24
20+
21+
right_1 = 17
22+
right_2 = 27
23+
24+
print(solution(left_1, right_1))
25+
print(solution(left_2, right_2))
26+
27+
import math
28+
def solution_other(left, right):
29+
answer = 0
30+
31+
for cost in range(left, right + 1):
32+
sqrt = math.sqrt(cost)
33+
if int(sqrt) == sqrt:
34+
answer -= cost
35+
else:
36+
answer += cost
37+
38+
return answer
39+
40+
print(solution_other(left_1, right_1))
41+
print(solution_other(left_2, right_2))
42+
43+
def solution_best(left, right):
44+
answer = 0
45+
46+
for cst in range(left, right + 1):
47+
if int(cst ** 0.5) == cst ** 0.5:
48+
answer -= cst
49+
else:
50+
answer += cst
51+
52+
return answer
53+
54+
print(solution_best(left_1, right_1))
55+
print(solution_best(left_2, right_2))

LEVEL2/Day3/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))

0 commit comments

Comments
 (0)