Python
-
백준(BeakJoon)_1260_DFS와BFSPython/Coding_Problems 2022. 12. 18. 12:05
# DFS를 재귀로 풀었을 때 from collections import deque def DFS(graph, visited, start_node): visited[start_node] = True print(start_node, end=" ") for i in graph[start_node]: if not visited[i]: DFS(graph,visited,i) def BFS(graph, visited, start_node): queue = deque() queue.append(start_node) # 노드를 qeue에 넣어준다 visited[start_node] = True while queue: current_node = queue.popleft() print(current_node, end=" ..
-
reduce함수Python/Basic 2022. 12. 11. 20:41
reduce함수 # syntax reduce(집계함수, 순회가능한데이터) - functools 내장 모듈의 reduce()함수는 여러개의 데이터를 대상으로 주로 누적 집게를 내기 위해서 사용 - 집계함수에서는 두개의 인자를 사용, 첫번째는 인자는 누적자(accumulator), 두번째 인자는 현재값(current value)가 넘어온다 누적자(accumulator)는 함수 시작부터 끝까지 계속 재사용되는 값이고 현재값은 '순회가능한데이터' 에서 바뀌는 값 - 순회가능한데이터는 문자열, 리스트, 튜플... 등 시퀀스를 말한다 # reduce함수 from functools import reduce def add_func(a,b): return a + b def mul_func(a,b): return a * ..
-
-
-
-
-
모듈(module)Python/Basic 2022. 12. 11. 16:38
모듈(module)이란 변수, 함수, 클래스 등을 모아놓은 파일 # calculator.py # calculator 모듈 # 합 def sum(x, y): return x + y # 차이 def difference(x, y): return x - y # 곱 def product(x, y): return x * y # 제곱 def square(x): return x * x # test.py from calculator import sum # calculator 모듈에서 sum 함수 부르기 print(sum(5,10)) from calculator import * print(sum(3, 5)) print(difference(3, 5)) print(product(3, 5)) print(square(3)) 파이..