알고리즘

알고리즘

[파이썬]프로그래머스 Lv.1 완주하지못한선수

✅ 코드 def solution(participant, completion): tmp = {} for player in participant: tmp[player] = tmp.get(player, 0) + 1 for fin_player in completion: tmp[fin_player] -= 1 if tmp[fin_player] == 0: del tmp[fin_player] return list(tmp.keys())[0] participant 리스트에는 동명이인이 존재하기 때문에 집합 자료형은 사용하지 않았습니다. 대신에 동명이인의 수를 카운팅 할 수 있는 딕셔너리 자료형을 사용했습니다. 그렇게 리스트의 정보를 딕셔너리로 변환한 결과 문제를 해결할 수 있었습니다. 그 과정에서 자바스크립트 객체의 초기..

알고리즘

[파이썬]프로그래머스 Lv.1 다트게임

2018 카카오 블라인드 채용 def solution(dartResult): scores = [] dartResult = dartResult.replace('10', 'k') for x in dartResult: if x.isdecimal(): scores.append(int(x)) elif x.isalpha(): if x == 'D': scores[-1] **= 2 elif x == 'T': scores[-1] **= 3 elif x == 'k': scores.append(10) else: if x == '*': scores[-2:] = [x*2 for x in scores[-2:]] elif x == '#': scores[-1] *= -1 return sum(scores) 주어진 조건을 잘 이해하면 ..

알고리즘

[파이썬]프로그래머스 Lv.1 소수만들기

✅ 코드 from itertools import combinations import math def is_prime(x): for i in range(2, int(math.sqrt(x)) + 1): if x % i == 0: return False return True def solution(nums): answer = 0 combis = list(combinations(nums, 3)) for combi in combis: if is_prime(sum(combi)): answer += 1 return answer 주어진 배열에서 3개의 수를 조합해 더하면 소수가 되는 케이스의 수를 구해야 합니다. 다행스럽게도(?) 파이썬의 itertools 모듈을 사용하면 수들의 조합을 구할 수 있습니다. 소수 여부를..

알고리즘

[파이썬]프로그래머스 Lv.1 같은숫자는싫어

✅ 코드 def solution(arr): answer = [] for num in arr: if answer[-1:] == [num]: continue answer.append(num) return answer 빈 배열을 슬라이싱해도 IndexError가 발생하지 않습니다. 반대로 특정 위치의 값을 인덱싱 하면 IndexError가 발생합니다.

알고리즘

[파이썬]프로그래머스 Lv.1 달리기경주

✅ 코드 def solution(players, callings): players_dict = {player: idx for idx, player in enumerate(players)} for name in callings: idx = players_dict[name] front = players[idx - 1] players[idx - 1], players[idx] = players[idx], players[idx - 1] players_dict[name], players_dict[front] = idx - 1, idx return players 입력값의 제한은 다음과 같습니다. 5

알고리즘

[파이썬]프로그래머스 Lv.1 추억점수

✅ 코드 def solution(name, yearning, photos): answer = [] score = dict(zip(name, yearning)) for photo in photos: tmp = 0 for person in photo: if person in score: tmp += score[person] answer.append(tmp) return answer 문제가 제시하는 조건은 다음과 같습니다. 3

truezero
'알고리즘' 태그의 글 목록