전체 글
-
파이썬, class, Exception, 시간복잡도 공간복잡도, Big-O NotationPython 2024. 7. 24. 09:05
class 파이썬은 그 자체로 객체라고 할 수 있을만큼 대표적인 객체 지향 프로그래밍(OOP) 언어이며, class는 객체 지향 프로그래밍에서 사용되는 중요한 개념이다. class는 데이터 타입을 만든다는 거고, class에는 '속성'과 '행동'이 있어야 한다. 속성은 class 안에서 정의된 변수 → 즉, 객체가 가지고 있는 '데이터'행동은 class 안에서 정의된 함수 → 즉, '메서드' [ 메서드의 종류 ] 1. 인스턴스 메서드우리가 주로 사용하는 메서드로, 인스턴스 번수를 사용하거나, 인스턴스 변수에 값을 설정한다.클래스 내부에 정의되는 기본 메서드다.호출 시, 첫번째 인자로 인스턴스 자기자신(self))가 자동으로 전달된다.메서드를 호출한 인스턴스를 의미하는 self 매개변수를 통해 인스턴..
-
파이썬 자료구조 알고리즘, BFS -> Queue로 구현Python 2024. 7. 23. 01:46
BFS 저번시간에 배운 DFS는 깊이 우선 탐색으로, 탐색하는 노드를 갈 수 있는 한 최대한 깊이 따라갔다가 더이상 길이 없으면 백해서 다시 노드를 탐색하는 구조였다면 BFS는 Breadth-first search, 너비 우선 탐색이라는 뜻으로, 현재 노드를 시작으로 현재 노드에 인접한 모든 노드들을 먼저 방문한다. 즉, 아래 그래프에서 DFS가 1 > 2 > 5 > 9 > 3 순서로 방문한다면, BFS는 1 > 2 > 3 > 4 > 5 순서로 방문한다. 이번에도 코드로 구현해보자.우선 딕셔너리로 그래프를 작성하고graph = {1: [2, 3, 4],2: [5],3: [6, 7],4: [8],5: [9],6: [10],7: [],8: [],9: [],10: [],} Queue를 사용할거다. 일단 BFS..
-
[WIL] 시간이 왜 이렇게 빠를까thoughts💭 2024. 7. 21. 23:45
벌써 7월 마지막주가 다가온다.알고리즘은 여전히 진도가 더디고 빅분기 시험은 점점 다가오고 한숨은 잦아진다.'강의를 다 들을 수 있을까'에 대한 불안한 마음을 가지게 되리라고는 생각하지 못했는데..한 번 듣는걸로는 이해가 잘 안되니 몇 번 반복하고.. 예제를 풀어야 이해가 되니까 예제를 풀어야 하는데 예제는 강의보다 더 어렵고.. 그러니 진도가 느려진다..코드카타 문제와 스쿼드 과제는 쉬운것도 있고 어려운것도 있지만 생각을 해야해서 시간이 걸린다. 그래도 생각하는 과정이 재밌고 실행했을 때 에러가 나든 잘 되든 결론이 있는게 좋다. 하루가 48시간이었으면.ㅠㅠ 그래도 알고리즘은 재밌으니 한 주 더 했음 좋겠다ㅠㅠ 따라가지 못하는 내 실력과 속도가 아쉽다.
-
파이썬 자료구조 알고리즘, DFS -> 재귀함수로 구현Python 2024. 7. 21. 20:55
DFS Depth First Search 깊이 우선 탐색 이라는 뜻으로, 아래와 같이 노드가 갈 수 있는 깊이만큼 계속 가다가 길이 없으면 다시 백해서 다른 방향을 탐색해서 가는 구조를 말한다. DFS를 구현하는 방법은 재귀함수로 구현하는 방법과 스택으로 구현하는 방법 두가지가 있는데 재귀함수로 코드를 짤 때, 가장 중요한 포인트는 두가지이다. 1. 반복적으로 발생하는 일이 뭔지 아는 것2. 종료 조건을 아는 것각각의 숫자는 노드 노드 1을 방문하고 2, 5, 9 차례대로 방문할거다. 근데 2에 방문 했는데 2한테 자식이 있으니까 5한테 가기 전에 2의 자식인 3한테 가고 3의 자식인 4한테 갔다가 더이상 자식이 없음을 확인 후 백해서 2로 온 후 다른 자식이 있는지 확인하고 없으면 5한테 가는 순이다..
-
파이썬 자료구조 알고리즘, 코딩테스트 문제, n번째 피보나치 수 구하기Python 2024. 7. 19. 19:00
자료구조 & 알고리즘 그래프자료구조는 크게 비선형구조, 선형구조로 구분된다.선형은 말 그대로 선 Line 처럼 생긴 그래프이고 자료를 저장하고 꺼내는 것에 초점이 맞춰져 있다. 비선형은 이런 구조의 그래프를 말한다. [그래프에서 사용되는 용어] 1. 노드(Node) : 연결관계를 가진 각 데이터를 의미한다. 정점(Vertex)라고도 함2. 간선(Edge) : 노드 간의 관계를 표시한 선3. 인접 노드(Adjacent Node): 간선으로 직접 연결된 노드(또는 정점) 예를 들어 이렇게 연결 되어있다고 했을 때 제니, 연우, 지수, 사나는 각각 노드이들을 연결하는 선들은 엣지지수와 연우는 인접해있고, 제니와 지수는 인접해있지 않다. [그래프 유형]유방향 그래프(Directed Graph): 방향이 ..
-
파이썬, 자료구조 알고리즘, is_pelindrome codePython 2024. 7. 19. 12:33
자료구조 알고리즘 array : 파이썬의 리스트. 접근 쉬움, 삽입 어려움.linked list (연결리스트) : 직접 구현. 접근 어려움, 삽입 쉬움. ArrayLInked List특정 원소 조회O(1)O(N)중간에 삽입, 삭제O(N)O(1)데이터 추가데이터 추가 시 모든 공간이 다 차버렸다면 새로운 메모리 공간을 할당받아야 한다모든 공간이 다 찼어도 맨 뒤의 노드만 동적으로 추가하면 된다.정리데이터에 접근하는 경우가 빈번하다면 Array를 사용하자삽입과 삭제가 빈번하다면 LinkedList를 사용하는 것이 더 좋다. class class에는 매직 매소드인 __init__ 이 들어가는데 가장 먼저 꼭 실행되고, 얘는 첫번째 파라미터로 항상 self를 받는다. 파이썬은 self를 알아서 넣어주기 때문에..
-
파이썬 알고리즘 코딩 문제 풀기(programmers)Python 2024. 7. 16. 21:18
25. 나누어 떨어지는 숫자 배열array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요.divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하세요. 일단, 문제 그대로 for문을 돌면서 divisor로 나눴을 때 떨어지는 값들을 요소로 받는 리스트 valid_list를 생성했다.if 문으로 divisor로 나누어 떨어지는 element가 하나도 없으면 [-1] 반환하는 코드를 추가하고새로 만들어준 valid_list()를 오름차순으로 정렬한 후 반환해주면 끝def solution(arr, divisor): # arr를 돌면서, i를 divisor로 나누었을때 딱 떨어지는 값 i..
-
파이썬 자료구조 알고리즘, programmers 코딩 기초 트레이닝Python 2024. 7. 15. 03:57
Queue First In First Out 자료구조라고 해서 FIFO 라고 부른다. 선입선출이라고 이해하면 된다. 놀이기구 줄 처럼 처음에 줄 선 사람이 가장 빨리 타고 마지막에 줄 선 사람이 가장 늦게 타는 구조라고 생각하면 쉽다. 그럼 큐를 활용한 예제 문제를 풀어보자.근데 문제를 풀기 전에 deque를 알아야 한다. deque는 더블 엔디드 큐(Double-Ended Queue)의 줄임말로, 양쪽 끝에서 빠르게 삽입과 삭제를 할 수 있는 자료 구조를 뜻한다. list와 비슷하지만 성능면에서 훨씬 높은 효율성을 가진다. deque 사용법from collections import deque# deque 생성deq = deque()# 요소 추가deq.append # 오른쪽 끝에 추가deq.a..