본문 바로가기

Algorithm48

[프로그래머스] 체육복 (javascript) Javascript 프로그래머스 솔루션 function solution(n, lost, reserve) { let realLost = lost.filter(a => !reserve.includes(a)); let realReserve = reserve.filter(a => !lost.includes(a)); return n - realLost.filter(a => { let b = realReserve.find(r => Math.abs(r-a) r !== b); }).length; } 출처: jsikim1.tistory.com/52 1. what is "filter" in js? filter() The filter() method creates a new array with all elements tha.. 2021. 1. 23.
[프로그래머스] 체육복 (python) Python 주의사항은 여벌을 가지고 있는 리스트에서도 도난이 발생할 수 있기 때문에 lost와 reserve 각각에 대해서 중복이 없게 차집합을 만들어 놓아야 한다. (first two lines of codes.) 그리고 여벌 리스트(rsv_pos)를 loop돌면서 도난 당한 리스트(lost_pos)의 앞 or 뒤에 여벌리스트의 요소들이 있는지 체크하면서 도난 리스트를 제거한다. python remove method는 list에서 remove에 주어진 item을 찾아서 삭제하는 메서드이다. 만약, 해당 아이템이 없다면 "ValueError: list.remove(x): x not in list" 이러한 에러 메시지를 보게될 것이다. def solution(n, lost, reserve): rsv_po.. 2021. 1. 18.
[프로그래머스] 모의고사 (python, javascript) 출처: wooaoe.tistory.com/65 def solution(answers): answer = [] answer_temp = [] count01 = 0 count02 = 0 count03 = 0 one = [1,2,3,4,5] two = [2,1,2,3,2,4,2,5] three = [3,3,1,1,2,2,4,4,5,5] for i in range(len(answers)): if answers[i] == one[i%len(one)]: count01+=1 if answers[i] == two[i%len(two)]: count02+=1 if answers[i] == three[i%len(three)]: count03+=1 answer_temp = [count01, count02, count03] f.. 2021. 1. 17.