def solution(s) :
stack = []
for alphabet in s :
if stack and stack[-1] == alphabet:
stack.pop()
else :
stack.append(alphabet)
return 1 * (not stack)
def solution(s) :
answer = [0, 0]
while s != '1' :
num_zeros = s.count('0')
s = bin(len(s) - num_zeros)[2:]
answer[0] += 1
answer[1] += num_zeros
return answer
def solution(n):
stack = [1]
answer = 0
sum_stack = 1
i = 2
while i <= n + 1:
if sum_stack == n:
answer += 1
sum_stack -= stack.pop(0)
if sum_stack < n:
stack.append(i)
sum_stack += i
i += 1
else:
sum_stack -= stack.pop(0)
return answer
from functools import reduce
from fractions import gcd
def solution(arr = [2,3, 4]):
gcdArr = reduce(gcd, arr, arr[0])
return reduce(lambda x, y : x*y//gcd(x, y), arr, gcdArr)
def solution(s):
return ' '.join([word.capitalize() for word in s.split(" ")])
capitalize
: ๋จ์ด์ ์ฒซ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์
import re
from collections import Counter
def solution(s) :
s = Counter(re.findall('\d+', s))
return list(map(int, sorted(s, key=lambda k: -s[k])))
def dfs(numbers, index=0, total=0, target=0):
if index == len(numbers):
return 1 if total == target else 0
number = numbers[index]
return dfs(numbers, index + 1, total + number, target) + dfs(numbers, index + 1, total - number, target)
def solution(numbers, target):
return dfs(numbers, target=target)
์ฟผ๋์์ถ ํ ๊ฐ์ ์ธ๊ธฐ
import numpy as np
def solution(arr):
def fn(a):
if np.all(a == 0): return np.array([1, 0])
if np.all(a == 1): return np.array([0, 1])
n = a.shape[0]//2
return fn(a[:n, :n]) + fn(a[n:, :n]) + fn(a[:n, n:]) + fn(a[n:, n:])
return fn(np.array(arr)).tolist()
def solution(brown, yellow) :
a = 1
b = -(brown+4) / 2
c = brown + yellow
discriminat = (b**2 - 4 * a * c)**0.5
return [(-b + discriminat)/2*a, (-b - discriminat)/2*a]