Today : 16
주사위 게임3
def solution(a, b, c, d):
l = [a,b,c,d]
c = [l.count(x) for x in l]
if max(c) == 4:
return 1111*a
elif max(c) == 3:
return (10*l[c.index(3)]+l[c.index(1)])**2
elif max(c) == 2:
if min(c) == 1:
return eval('*'.join([str(l[i]) for i, x in enumerate(c) if x == 1]))
else:
return (max(l) + min(l)) * abs(max(l) - min(l))
else:
return min(l)
list에서는 .count로 특정 x의 개수 count 가능하므로 -> max로 경우의 수 나누기 (나는 set을 활용함)
.index를 쓰면 그것의 index를 구할 수 있음.
count에 리스트 형태로 저장되어있으므로 원하는 수만큼 저장된 숫자의 index를 구해서 list에서 해당하는 숫자를 선택 가능.
내 답안
def solution(a, b, c, d):
cnt_list = [a,b,c,d]
sorted_list = sorted(cnt_list)
if len(set(cnt_list)) == 1:
return 1111*a
elif len(set(cnt_list)) == 4:
return min(cnt_list)
elif len(set(cnt_list)) == 3:
for i in range(len(sorted_list)-1):
if sorted_list[i] == sorted_list[i+1]:
del sorted_list[i]
del sorted_list[i]
return sorted_list[0] * sorted_list[1]
else:
for i in range(len(sorted_list)-1):
if sorted_list[i] != sorted_list[i+1]:
if i == 1:
return (sorted_list[i] + sorted_list[i+1]) * abs(sorted_list[i] - sorted_list[i+1])
elif i == 0:
return (10*sorted_list[i+1] + sorted_list[i])**2
else:
return (10*sorted_list[i] + sorted_list[i+1])**2
모든 경우의 수를 하나하나 다룬 모습..
배열 만들기2
def solution(l, r):
answer = []
for num in range(l, r + 1):
if not set(str(num)) - set(['0', '5']):
answer.append(num)
return answer if answer else [-1]
set은 생각해봤는데 0이랑 5만 뺄 방법이 생각이 안났다..
이렇게 set을 str에 적용할 수 있는지 그리고 set끼리 뺄 수 있는줄 몰랐음..!
이걸 노가다로 하면 내 부끄러운 코드다.
내 답안
def solution(l, r):
answer = []
num = ['1','2','3','4','6','7','8','9']
for x in range(l,r+1):
if '0' in str(x) or '5' in str(x):
for i in num:
if '1' not in str(x):
if '2' not in str(x):
if '3' not in str(x):
if '4' not in str(x):
if '6' not in str(x):
if '7' not in str(x):
if '8' not in str(x):
if '9' not in str(x):
answer.append(x)
break
return answer if answer else [-1]
'코테' 카테고리의 다른 글
250402 프로그래머스 파이썬 Lv.0 (0) | 2025.04.02 |
---|---|
250330 프로그래머스 파이썬 Lv.0 (0) | 2025.03.31 |
댓글