[Python] boolean을 int로 변환하기
·
✏️ Study/🐍 Python
📍백준 13458 https://www.acmicpc.net/problem/13458 13458번: 시험 감독첫째 줄에 시험장의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 각 시험장에 있는 응시자의 수 Ai (1 ≤ Ai ≤ 1,000,000)가 주어진다. 셋째 줄에는 B와 C가 주어진다. (1 ≤ B, C ≤ 1,000,000)www.acmicpc.net boolean을 int로 변환하여 사용할 수 있다boolean값에 int를 씌우면- true => 1- false => 0 으로 변환된다 N = int(input())data = list(map(int, input().split()))B, C = map(int, input().split())answer = len(data)f..
[Python] 최종 원소가 int인 2차원 리스트 만들기
·
✏️ Study/🐍 Python
📍참고https://www.acmicpc.net/problem/9063 9063번: 대지첫째 줄에는 점의 개수 N (1 ≤ N ≤ 100,000) 이 주어진다. 이어지는 N 줄에는 각 점의 좌표가 두 개의 정수로 한 줄에 하나씩 주어진다. 각각의 좌표는 -10,000 이상 10,000 이하의 정수이다. www.acmicpc.net 📍코드import sysN = int(input())# data = [ [x1, y1], [x2, y2], ... ]data = [list(map(int, sys.stdin.readline().rstrip().split())) for i in range(N)]xs = []ys = []for row in data: x, y = row xs.append(x) y..
[Python] 백준 2346 < 풍선 터뜨리기 >
·
✏️ Study/🐍 Python
📍문제 링크https://www.acmicpc.net/problem/2346 2346번: 풍선 터뜨리기1번부터 N번까지 N개의 풍선이 원형으로 놓여 있고. i번 풍선의 오른쪽에는 i+1번 풍선이 있고, 왼쪽에는 i-1번 풍선이 있다. 단, 1번 풍선의 왼쪽에 N번 풍선이 있고, N번 풍선의 오른쪽에 1번 풍선www.acmicpc.net 📍알고리즘 분류- 자료구조- 덱 📍문제 풀이- 1부터 N까지의 원형 리스트가 있고, 각 원소는 -N 부터 N 중 0을 제외한 정수를 값으로 갖는다1부터 시작하여 각 정수만큼 왼쪽, 오른쪽으로 이동하며 터지는 풍선의 번호를 순서대로 출력한다 📍코드from collections import dequeN = int(input())# list로는 바로 생성 불가d = de..
[Python] global, nonlocal
·
✏️ Study/🐍 Python
파이썬에서는 하위 스코프에서 상위 스코프의 변수를 영구 변경 가능 📍Global- 전역 변수를 하위 스코프에서 변경할 때 사용- 예시a = "outer"print(a) # outerdef print_out(): global a # a 를 전역 변수로 사용한다고 선언 a = "inner" # a 변경 print(a)print_out() # innerprint(a) # inner 📍nonlocal- 중첩 함수 내에서 지역 변수로 사용하지 않을 때 사용(상위 스코프의 비전역 변수를 탐색)def print_out(): num = 0 def change_num(): nonlocal num num = 100 print(num) cha..
[Python] deque
·
✏️ Study/🐍 Python
📍참고https://docs.python.org/ko/3/library/collections.html#deque-objects collections — Container datatypesSource code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.,,...docs.python.org 📍Deque 란?- 스택 + 큐- head, tail에서 원소 추가, 삭제가 O(1)에 가능- 예시 from collections imp..
[Python] range(a, b) 에서 a == b 인 경우 주의하기
·
✏️ Study/🐍 Python
for loop를 쓸 때 range(1, 1) 처럼 수가 같은 경우, for loop가 동작하지 않는다 📍예시백준 1292번 쉽게 푸는 문제start, end = map(int, input().split())sequence = []def solution(s, e, arr): count = 0 for i in range(1, e + 1): # 1 1 이 입력으로 주어졌을 때 range(1, 1) 이면 반복문 실행 안됨 for j in range(i): arr.append(i) count += 1 if count == end: return sum(arr[s - 1:e])print(solution(..