import math
def solution(n):
return n / math.gcd(6,n)
def solution(my_string, num1, num2):
lst = list(my_string)
lst[num1] = my_string[num2]
lst[num2] = my_string[num1]
return "".join(lst)
def solution(n):
return [i for i in range(1,n+1) if n % i == 0]
def solution(n):
result = 0
for i in range(n):
if ((result + 1) % 3 == 0) | ((result % 10) - 2 == 0):
result += 2
if (result % 10) - 3 == 0:
result += 1
elif result % 3 == 0:
result += 1
else:
result += 1
if result == 31:
result += 9
elif result == 130:
result += 10
return result
def solution(n):
answer = 0
for _ in range(n):
answer += 1
while answer % 3 == 0 or '3' in str(answer):
answer += 1
return answer
def solution(polynomial):
lst = polynomial.split(" + ")
coef = 0
const = 0
for s in lst:
if "x" in s:
if s[:-1] == "":
coef += 1
else:
coef += int(s[:-1])
else:
const += int(s)
if coef == 1:
coef = "x"
elif coef == 0:
pass
else:
coef = str(coef)+"x"
if const == 0:
return coef
elif coef == 0:
return str(const)
else:
return coef + " + " + str(const)
def solution(array):
count = {}
result = 0
for n in array:
count[n] = array.count(n)
count_lst = sorted(count.items(), key=lambda x : -x[1])
if len(count_lst) >= 2:
if count_lst[0][1] == count_lst[1][1]:
result = -1
else:
result = count_lst[0][0]
else:
result = count_lst[0][0]
return result
다른 사람 풀이
잘 이해가 안돼서 직접 코드를 뜯어가면서 이해했다..
최종 1개의 원소가 남을 때까지 순차적으로 array 내의 모든 원소를 제거해가면서 최빈값만 남기게 됨. 최빈값이 여러개라면 i != 0
이므로 -1을 return.
def solution(array):
while len(array) != 0:
for i, a in enumerate(set(array)):
array.remove(a)
if i == 0: return a
return -1
def solution(quiz):
result = []
for q in quiz:
lst = q.split()
if lst[1] == "+":
answer = int(lst[0]) + int(lst[2]) == int(lst[-1])
else:
answer = int(lst[0]) - int(lst[2]) == int(lst[-1])
if answer is True:
result.append("O")
else:
result.append("X")
return result
eval()
을 이용하면 간단하게 풀 수 있다!def valid(equation):
equation = equation.replace('=', '==')
return eval(equation)
def solution(equations):
return ["O" if valid(equation) else "X" for equation in equations]
def solution(common):
diff_sub = common[-1] - common[-2]
result = 0
if diff_sub == common[-2] - common[-3]:
result = common[-1] + diff_sub
else:
diff_prod = common[-1] // common[-2]
result = common[-1] * diff_prod
return result
def solution(common):
answer = 0
a,b,c = common[:3]
if (b-a) == (c-b):
return common[-1]+(b-a)
else:
return common[-1] * (b//a)
return answer