Programmers / 2단계 / 모음사전 / python
·
코딩테스트/programmers (python)
https://school.programmers.co.kr/learn/courses/30/lessons/84512 나의 풀이 . word의 길이와 각 알파벳의 인덱스 값을 이용해 결과를 찾아낼 수 있을 것이라고 생각하였으나 어려움이 있었다. 모범 답안 중복 순열 이용 from itertools import product def solution(word): words = [] for i in range(1, 6): for c in product(['A', 'E', 'I', 'O', 'U'], repeat=i): words.append(''.join(list(c))) words.sort() return words.index(word) + 1 단순히 중복 순열을 이용하여 sort함수로 정렬을 하면 되는 문제였..
Programmers / 2단계 / 타겟 넘버 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - 타겟 넘버 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 . 모범 답안 DFS로 구현 def dfs(numbers, target, idx, values): # idx : 깊이 / values : 더하고 뺄 특정 leaf 값 global cnt cnt = 0 if idx == len(numbers) & values == target : # 깊이가 끝까지 닿았으면 cnt += 1 return elif idx == len(numbers) : # 끝까지 탐색했는데 sum이 target과 다르다면 그냥 넘어간다 return # 재귀함수로 구현 dfs(numbers, target, idx+1, values + numbers[idx]) # 새로운 value 값 세팅 dfs(n..
Programmers / 2단계 / 캐시 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - [1차] 캐시 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 . 모범 답안 def solution(cacheSize, cities): cache = [] time = 0 for city in cities: city = city.lower() if cacheSize: if not city in cache: if len(cache) == cacheSize: cache.pop(0) cache.append(city) time += 5 else: cache.pop(cache.index(city)) cache.append(city) time += 1 else: time += 5 return time LRU : 가장 오랫동안 참조되지 않은 페이지를 교체 LRU 예시) 캐시크기..
Programmers / 2단계 / 의상 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - 의상 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 . 모범 답안 def solution(clothes): clothes_type = {} for c, t in clothes: if t not in clothes_type: clothes_type[t] = 2 #첨에 딕셔너리에 해당 종류의 의상이 없을 때 그 의상을 더 해주면서 #아무것도 안입었을때의 경우를 함께 생각해서 2를 더해줍니다. else: clothes_type[t] += 1 cnt = 1 for num in clothes_type.values(): cnt *= num return cnt - 1 def solution(clothes): hash_map = {} for clothe, type in clot..
Programmers / 2단계 / 가장 큰 수 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - 가장 큰 수 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 def solution(numbers): string='' number = list(map(str, numbers)) num = number.sort(key = lambda x: x[0], reverse=True) for i in num: string+=i return string 모범 답안 def solution(numbers): numbers = list(map(str, numbers)) numbers.sort(key=lambda x: x*3, reverse=True) return str(int(''.join(numbers))) 먼저 30과 3, 39 라는 수가 있다고 가정합니다. 원하는 결과값을 얻기..
Programmers / 2단계 / 행렬의 곱셈 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - 행렬의 곱셈 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 . 모범 답안 def solution(arr1, arr2): answer = [[0 for _ in range(r)] for _ in range(m)] for i in range(len(arr1)): for k in range(len(arr2[0])): for j in range(len(arr1[0])): answer[i][k] += arr1[i][j] * arr2[j][k] return answer
Programmers / 2단계 / H-Index / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - H-Index | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 . 모범 답안 def solution(citations): citations.sort() l = len(citations) for i in range(l): if citations[i] >= l-i: return l-i return 0 문제 이해에 어려움이 있었다.
Programmers / 2단계 / n^2 배열 자르기 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - n^2 배열 자르기 | 프로그래머스 스쿨 (programmers.co.kr) 나의 풀이 def solution(n, left, right): arr = [[0] * n for _ in range(n) ] num=1 for i in range(n): for j in range(n): if i==j: arr[i][j]=num num+=1 else: arr[i][j]=num new_arr=sum(arr, []) return new_arr[left:right+1] 중간 중간 출력하여 확인하며 문제를 풀었는데 테스트 실패가 떴다. 뒤늦게 입출력 예시를 보니 문제가 내가 이해한 것과 달랐다. 나는 n=3일 때, i행 i열까지 영역 내의 모든 빈 칸을 숫자 i로 채울 때 다음과 같은 2차원 배열을..