본문 바로가기

프로그래머스 알고리즘 문제

(16)
프로그래머스 Level 1 - 예산 내 정답: 그리디 사용 def solution(d, budget): count = 0 while True: if len(d)
프로그래머스 Level 1 - 콜라츠 추측 내 정답: def solution(num): count = 0 while not num == 1 and count < 500: if num%2: num = num * 3 + 1 else: num /= 2 count += 1 return count if count < 500 else -1 다른 사람들의 정답: def collatz(num): for i in range(500): num = num / 2 if num % 2 == 0 else num*3 + 1 if num == 1: return i + 1 return -1
프로그래머스 Level 1 - 키패드 누르기 내 정답: def solution(numbers, hand): keypad = [ [1,2,3], # [0][0], [0][1], [0][2] [4,5,6], # [1][0], [1][1], [1][2] [7,8,9], # ... 움직여야 할 거리는 abs(i1-i2)+abs(j1-j2) 이다. [98,0,99] ] def find_i_j(num): for i in range(4): for j in range(3): if keypad[i][j] == num: return i,j def dists_l_r(i_n,i_l,i_r,j_n,j_l,j_r): return abs(i_n-i_l)+abs(j_n-j_l), abs(i_n-i_r)+abs(j_n-j_r) answer = '' i_n, j_n, i_l, j..
프로그래머스 Level 1 - 2016년 내 정답: def solution(a, b): days = [31,29,31,30,31,30,31,31,30,31,30,31] yoil = ['THU','FRI','SAT','SUN','MON','TUE','WED'] numday = b for i in range(a-1): numday += days[i] return yoil[numday%7] 다른 사람들의 정답: 나와 비슷한 방식, 그러나 sum함수를 이용하여 좀 더 간결하게 def getDayName(a,b): months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] days = ['FRI', 'SAT', 'SUN', 'MON', 'TUE', 'WED', 'THU'] return days[(sum(mon..
프로그래머스 Level 1 - 체육복 내 정답: def solution(n, lost, reserve): l_and_r = list(set(lost).intersection(set(reserve))) for i in l_and_r: lost.remove(i) reserve.remove(i) print(lost, reserve) answer = n - len(lost) for i in list(lost): if reserve.count(i-1) > 0: lost.remove(i) reserve.remove(i-1) answer += 1 elif reserve.count(i+1) > 0: lost.remove(i) reserve.remove(i+1) answer += 1 return answer 다른 사람들의 정답: def solution(n,..
프로그래머스 Level 1 - 모의고사 내 정답: def solution(answers): def one(i): return (i + 1) % 5 or 5 def two(i): if i % 2 == 0: return 2 else: if i % 8 == 1: return 1 elif i % 8 == 3: return 3 elif i % 8 == 5: return 4 else: return 5 def three(i): if i % 10 < 2: return 3 elif i % 10 < 4: return 1 elif i % 10 < 6: return 2 elif i % 10 < 8: return 4 else: return 5 answer = [] oneCount = 0 twoCount = 0 threeCount = 0 for i in range(l..
프로그래머스 Level 1 - 완주하지 못한 선수 내 정답: def solution(participant, completion): participant.sort() completion.sort() for i in range(len(participant)-1, -1, -1): if participant[i] == completion [i-1]: continue else: return participant[i] 다른 사람의 정답: collections 패키지 사용 import collections def solution(participant, completion): answer = collections.Counter(participant) - collections.Counter(completion) return list(answer.keys())[0] 해시..
프로그래머스 Level 1 - 크레인 인형 뽑기 내가 제출한 정답: def solution(board, moves): answer = 0 stack = [] def checkStack(stck): length = len(stck) if length < 2: return False elif stck[length-1] == stck[length-2]: stck.pop() stck.pop() return True return False for move in moves: for row in board: if row[move-1]: stack.append(row[move-1]) row[move-1] = 0 if checkStack(stack): answer += 2 break return answer 다른 사람의 정답들: def solution(board, m..