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]
(, ) ์ ๊ฐฏ์๊ฐ ์ง์ด๋ง๊ณ , ์ด๋ฆฐ๊ฒ ์๋๋ฐ ๋ซ๋ ๊ฒฝ์ฐ๋ฅผ ์ฒดํฌํฉ๋๋ค.
def solution(s) :
open_count = 0
for type in s :
if open_count <= 0 and type == ')' : return False
open_count += 1 if type == '(' else -1
return True and open_count == 0
from collections import defaultdict
from bisect import insort, bisect_left
def solution(info, query):
answer = []
table = [x.split(' ') for x in info]
queries = [x.replace('and ', '') for x in query]
masks = [bin(x)[2:].zfill(4) for x in range(16)]
group = defaultdict(list)
for info_single in table :
condition, score = info_single[:-1], info_single[-1]
for mask in masks :
key = [x if m == '1' else '-' for x, m in zip(condition, mask)]
insort(group[' '.join(key)], int(score))
for query in queries :
score_index = query.rfind(' ')
condition, score = query[:score_index], query[score_index+1:]
scores = group[condition]
answer.append(len(scores) - bisect_left(scores, int(score)))
return answer
def solution(land) :
for h in range(1, len(land)) :
for w in range(4) :
prev_layer = land[h-1]
land[h][w] += max(prev_layer[:w] + prev_layer[w+1:])
return max(land[-1])