세상을 더 편리하게
article thumbnail
[Python/파이썬] 원소의 경우의 수 (순열, 조합)
Programming/Python 2021. 11. 25. 14:22

알고리즘 문제를 풀면 순열과 조합을 쓸일이 있다. 구현하는 것도 나쁘지 않지만 시간이 급박할 때에는 직접구현보다는 라이브러리를 활용하는 것이 좋다. import itertools 순열 서로 다른 n 개 중에 r개를 나열하는 경우의 수 import itertools arr = ['1', '2', '3', '4'] per = list(itertools.permutations(arr, 2)) for ele in per: print(ele) """ ('1', '2') ('1', '3') ('1', '4') ('2', '1') ('2', '3') ('2', '4') ('3', '1') ('3', '2') ('3', '4') ('4', '1') ('4', '2') ('4', '3') """ 중복순열 중복 가능한 n..

article thumbnail
[카카오 2021 인턴/파이썬] 미로 탈출
Programming/Python 2021. 9. 24. 20:45

0. 문제 https://programmers.co.kr/learn/courses/30/lessons/81304?language=python3 코딩테스트 연습 - 미로 탈출 4 1 4 [[1, 2, 1], [3, 2, 1], [2, 4, 1]] [2, 3] 4 programmers.co.kr 1. 해결 알고리즘 다익스트라 알고리즘, 비트마스크, 약간의 list & dict 차이 2. 해결 방법 2-1. 간선 문제를 읽어보면 간선의 방향이 중간 중간 바뀐다. 간선이 바뀌는 경우는 다음과 같다. A 노드 -> B 노드로 갈 때 예시로 들면 다음과 같다. 둘 다 일반 노드 일 경우 A가 활성화 된 함정노드, B가 일반 노드 A가 일반 노드, B가 활성화 된 함정노드 A, B 둘다 활성화 된 함정노드 * 활성화..

article thumbnail
[Python/파이썬] PriorityQueue & heapq / 우선순위큐와 힙큐
Programming/Python 2021. 8. 26. 17:24

백준에서 문제를 풀다가 Priority Queue 와 Heapq 차이로 문제 시간에 걸릴 수도 안 걸릴 수도 있다는걸 알았다. Priority Queue 와 Heaq의 차이를 알아보기 위해서 라이브러리를 뜯어서 맛보자 1. 라이브러리 속 코드 파이썬 3.9 버전 기준으로 queue 라이브러리 속 Priority Queue 는 아래와 같다. from heapq import heappush, heappop class PriorityQueue(Queue): '''Variant of Queue that retrieves open entries in priority order (lowest first). Entries are typically tuples of the form: (priority number, d..

[Python/파이썬]함수로 정렬하기(소스만)
Programming/Python 2021. 8. 22. 22:54

import functools def comparator(a,b): t1 = a+b t2 = b+a return (int(t1) > int(t2)) - (int(t1) < int(t2)) # t1이 크다면 1 // t2가 크다면 -1 // 같으면 0 def solution(numbers): n = [str(x) for x in numbers] n = sorted(n, key=functools.cmp_to_key(comparator)) answer = str(int(''.join(n))) return answer