[프로그래머스] 타겟 넘버 Python
·
개발💻/알고리즘
이제 슬슬 레벨2를 풀어볼 생각이다 DFS BFS 알고리즘 한창 할때는 진짜 잘했었는데 ㅠㅠㅠ 이젠 감도 잘 안잡힌다 그래도 힌트 하나도 안 얻고 스스로 재귀함수를 이용해서 해결했다!! 근데 여기서 answer쪽에서 에러가 났는데 전역변수가 아니라서 에러가 나서 global로 해줬는데 그것도 에러가 나서 찾아보니 아마 프로그래머스에서는 문제 풀이 자체가 함수로 정의되어있어서 global해도 에러가 나는거 같다 그때는 nonlocal을 쓰면 된다고 한다!! def solution(numbers, target): answer = 0 def plusOrminus(count, sum): nonlocal answer if count != len(numbers): plusOrminus(count+1, sum+num..
[프로그래머스] 모의고사 Python
·
개발💻/알고리즘
아직 레벨1 문제들을 풀면서 감각을 살리는 중이다 def solution(answers): answer = [] rate = [-1] + [0]*3 omr_1 = [1,2,3,4,5]*((len(answers) //5) +1) omr_2 = [2,1,2,3,2,4,2,5]*((len(answers) //8) +1) omr_3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]*((len(answers) // 10) +1) for i in range(len(answers)): if answers[i] == omr_1[i]: rate[1] +=1 if answers[i] == omr_2[i]: rate[2] +=1 if answers[i] == omr_3[i]: rate[3] +=1 max_value..
[프로그래머스] 체육복 Python
·
개발💻/알고리즘
잘 푼거같은데 자꾸 테스트 통과를 못해서 엄청 찾다가 인덱스가 문제라는걸 알았다 항상 range 든 list 든 탐색할때 0 부터 시작한다는걸 잊지말자!! def solution(n, lost, reserve): cloth = [1]*(n+1) for i in range(1,n+1): if i in lost and i in reserve: cloth[i] = 1 continue if i in lost: cloth[i] = 0 if i in reserve: cloth[i] = 2 print(cloth) answer = 0 for i in range(1, n+1): if cloth[i] == 0: if i > 1: if cloth[i - 1] == 2: cloth[i] +=1 cloth[i - 1] -=1 ..
[프로그래머스] 실패율 Python
·
개발💻/알고리즘
진짜 처음부터 하나하나 하려다보니 넘 어렵다.. 기초부터 다 찾아봐야하고 ㅠㅠ def solution(N, stages): rate = [0]*(N+1) man = len(stages) stage = 1 while stage 0: count = 0 for st in stages: if st == stage: count +=1 rate[stage-1] = count / man man -= count stage += 1 rate = rate[:-1] answer = [] while len(rate) != len(answer): max_value = max(rate) print(max_value) for i in range(len(rate)): if rate[i] == max_va..
[프로그래머스] 성격 유형 검사하기 Python
·
개발💻/알고리즘
감을 살리기 위해 기본문제부터 시작하겠다 반복문 조건문도 까먹어서 검색하면서 풀었다.. def solution(survey, choices): answer = '' result_table = {'R' : 0, 'T': 0, 'C':0,'F':0,'J':0,'M':0,'A':0,'N':0 } Length = len(survey) index = 0 while index 4: result_table[survey[index][1]] += choices[index] - 4 elif choices[index] < 4: result_table[survey[index][0]] += 4 - choices[index] else: pass index +=1 if resu..