본문 바로가기

python185

[LEET CODE] 206. Reverse Linked List # Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Constraints: The number of nodes in the list is the range [0, 5000]. -5000 ListNode: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) # Solution 2 - 반복 구조로 뒤집기 # Definition for singly-linked.. 2021. 3. 22.
[LEET CODE] 21. Merge Two Sorted Lists ( 연산자 우선순위, 변수 스왑) # Problem Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Constraints: The number of nodes in both lists is in the range [0, 50]. -100 ListNode: if (not l1) or (l2 and l1.val > l2.val): l1, l2 = l2, l1 # 작은 값을 계속 l1에 연결시킨다. if l1: l1.next = self.mergeTwoLists(l1.next, l2) return l1 # 연산자 우선순위 우선순위 연산자 설명.. 2021. 3. 22.
[LEET CODE] 234. Palindrome Linked List ( 연결리스트, 런너 기법) # Problem Given the head of a singly linked list, return true if it is a palindrome. Constraints: The number of nodes in the list is in the range [1, 105]. 0 bool: q: List = [] if not head: return True node = head # 리스트 변환 while node is not None: q.append(node.val) node = node.next # 팰린드롬 판별 # 시작과 끝 하나씩 비교 while len(q) > 1: if q.pop(0) != q.pop(): return False return True # Solution 2 - 데크를 이용한 .. 2021. 3. 22.
[코딩 + 알고리즘 완주반] 8일차. 예외 처리 # 예외 종류 # 파이썬 예외처리의 이해 # 예외 종류 # 문법적으로 에러가 없지만, 코드 실행(런타임)프로세스에서 발생하는 예외 처리도 중요 # linter : 코드 스타일, 문법 체크 # 1. SyntexError : 잘못된 문법 # print('Test) # '' 닫아줘야함 # if True # ':' 없음 # pass # x=> y # 없는 문법 (>=) # 2. NameError : 참조변수 없음 a=10 b=15 #print(c) # 3. ZeroDivisionError : 0 나누기 에러 #print(10/0) # 4. IndexError : 인덱스 범위 오버 x = [10,20,30] print(x[0]) # print(x[3]) # 예외 발생 # 5. KeyError dic = {'Nam.. 2021. 3. 22.
[코딩 + 알고리즘 완주반] 8일차. 파일 읽기, 쓰기 # txt 파일 읽기, 쓰기 # 파일 읽기 #파일 읽기, 쓰기 # 읽기 모드 : r ,쓰기 모드 (기존 파일 삭제) : w, 추가모드(파일 생성 또는 추가) : a # .. : 상대경로, . : 절대 경로 # 파일 읽기 # 예제 1 f = open('./resource/review.txt','r') content = f.read() print(content) print(dir(f)) # 반드시 close 리소스 반환 f.close() print('--------------------------------') print('--------------------------------') # 예제2 # with 문을 쓰면 close 안해도 파이썬에서 자동으로 함 with open('./resource/revie.. 2021. 3. 22.
[코딩 + 알고리즘 완주반] 7일차. 모듈, 패키지 # 모듈, 패키지 #패키지 생성 패키지 폴더 안에 __init__.py, fibonacci.py, prints.py, calculations.py 생성 # __init__.py # 용도 : 해당 디렉토리가 패키지임을 선언한다. # Python 3.x : 파일이 없어도 패키지 인식함 -> 하위호환 위해서 생성해놓는 것을 추천 #fibonacci.py class Fibonacci: def __init__(self,title = "fibonacci"): self.title = title def fib(n): a,b = 0,1 while a 2021. 3. 21.
반응형