1. ๋คํธ๊ฒ์
def solution(dart):
ans = []
for i in range(len(dart)):
if dart[i] =='S':
if dart[i-1] == '0' and dart[i-2] == '1':
ans.append(10)
else:
ans.append(int(dart[i-1]))
elif dart[i] == 'D':
if dart[i-1] == '0' and dart[i-2] == '1':
ans.append(10**2)
else:
ans.append(int(dart[i-1])**2)
elif dart[i] == 'T':
if dart[i-1] == '0' and dart[i-2] == '1':
ans.append(10**3)
else:
ans.append(int(dart[i-1])**3)
elif dart[i] == '*':
if len(ans) > 1:
ans[-1] *= 2
ans[-2] *= 2
else:
ans[-1] *= 2
elif dart[i] == '#':
ans[-1] *= -1
return sum(ans)
๋ถ์ > ๋จ์ ๊ตฌํ๋ฌธ์ ์๋ค. ์ ์๊ฐ 10์ธ ๊ฒฝ์ฐ๋ง ์ ์ฒ๋ฆฌํ๋ฉด ์ฝ๊ฒ ํ๋ฆฌ๋ ๋ฌธ์ ์๋ค.
2. ๋๋จธ์ง๊ฐ 1์ด ๋๋ ์ ์ฐพ๊ธฐ
def solution(n):
x = 1
while n % x != 1:
x += 1
return x
3. ๋ถ์กฑํ ๊ธ์ก ๊ณ์ฐํ๊ธฐ
def solution(price, money, count):
cnt = 0
for i in range(1,count+1):
cnt += price * i
if cnt <= money:
return 0
else:
return cnt - money
4. ๊ฐ์ด๋ฐ ๊ธ์ ๊ฐ์ ธ์ค๊ธฐ
def solution(s):
array = []
if len(s) % 2 == 0:
m = (len(s) // 2) - 1
k = (len(s) // 2)
array.append(s[m])
array.append(s[k])
return ''.join(array)
else:
m = (len(s)-1) // 2
return s[m]
5. ๊ฐ์ ์ซ์๋ ์ซ์ด
def solution(arr):
stack = []
stack.append(arr[0])
for i in range(1,len(arr)):
if stack[-1] != arr[i]:
stack.append(arr[i])
return stack
6. ๋๋์ด ๋จ์ด์ง๋ ์ซ์ ๋ฐฐ์ด
def solution(arr, divisor):
ans = []
for i in arr:
if i % divisor == 0:
ans.append(i)
if len(ans) == 0:
ans.append(-1)
ans.sort()
return ans
7. ๋ ์ ์ ์ฌ์ด์ ํฉ
def solution(a, b):
cnt = 0
arr = [a,b]
arr.sort()
if a == b:
return a
for i in range(arr[0],arr[1]+1):
cnt += i
return cnt
8. ๋ฌธ์์ด ๋ด ๋ง์๋๋ก ์ ๋ ฌํ๊ธฐ
def solution(strings, n):
ans = []
imp = []
for word in strings:
word = list(word)
ans.append(word)
ans.sort(key = lambda x: (x[n],x),reverse=False)
for i in ans:
imp.append(''.join(i))
return imp
9. ๋ฌธ์์ด ๋ด p์ y์ ๊ฐ์
def solution(s):
s = s.upper()
if s.count('P') == s.count('Y'):
return True
else:
return False
10. ๋น๋ฐ์ง๋
def solution(n, arr1, arr2):
imp = []
for i in range(len(arr1)):
arr1[i] = format(arr1[i],'b')
if len(arr1[i]) != n:
arr1[i] = ('0' * (n - len(arr1[i]))) + arr1[i]
for i in range(len(arr2)):
arr2[i] = format(arr2[i],'b')
if len(arr2[i]) != n:
arr2[i] = ('0' * (n - len(arr2[i]))) + arr2[i]
for k in range(len(arr1)):
ans = ''
for t in range(len(arr1[k])):
if arr1[k][t] == '1' or arr2[k][t] == '1':
ans += '#'
if arr1[k][t] == '0' and arr2[k][t] == '0':
ans += ' '
imp.append(ans)
return imp
๋ถ์ > ์ง์ ๋ณํ์ ์ ๋์ด๋ฅผ ์์จ ๋ ์ฌ์ฉํ๋ format ํจ์๋ฅผ ์๋กญ๊ฒ ์๊ฒ๋ ์ ๋ง ๋นผ๋ฉด, ๋ฌธ์ ์์ ์๊ตฌํ๋๋๋ก ๊ตฌํํ๋ฉด ์ฝ๊ฒ ํ๋ฆฌ๋ ๋ฌธ์ ์๋ค.
'Algorithm > ์ฝ๋ฉํ ์คํธ ์คํฐ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฝ๋ฉํ ์คํธ ์คํฐ๋ 4์ฃผ์ฐจ (0) | 2022.05.16 |
---|---|
์ฝ๋ฉํ ์คํธ ์คํฐ๋ 1์ฃผ์ฐจ (4/25 ~ 5/1) (0) | 2022.05.01 |