본문 바로가기

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

(10)
프로그래머스 Level 1 - [1차] 다트 게임 내 정답: 코드가 참 더럽다. def solution(dartResult): def power(str): if str == 'S': return 1 elif str == 'D': return 2 return 3 scores = [0,0,0] res = dartResult.replace('10', ' ten') for i in '0123456789': res = res.replace(i,' ' + i) res = res.replace('ten', '10') rstr = res.split() print(rstr) for i in range(3): isTen = False if rstr[i][1].isdigit(): isTen = True if isTen: sc = int(rstr[i][:2]) ** powe..
프로그래머스 Level 1 - 실패율 내 정답: def solution(N, stages): a = [] for i in range(1, N+1): success, tries = 0, 0 for stage in stages: if stage > i: success += 1 tries += 1 elif stage == i: tries += 1 if tries: a.append((success/tries,i)) else: a.append((1, i)) return [i[1] for i in sorted(a)] 다른 사람들의 정답: def solution(N, stages): result = {} denominator = len(stages) for stage in range(1, N+1): if denominator != 0: count = s..
프로그래머스 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..