[Python] divmod() 함수
divmod() 활용 예시 프로그래머스 Lv1~Lv2 (백준 기준 브론즈~실버) 코테에서 가독성 높이기용 보조 함수로 자주 등장“몫과 나머지를 동시에 써야 할 때”는 항상 divmod()가 더 깔끔 1. 시간 계산에 활용 (초를 분과 초로 변환)총 시간을 분과 남은 초로 나누어 계산할 때 유용합니다.# 초(total_seconds)를 분(minutes)과 초(seconds)로 변환하는 예제# divmod(x, y)는 (x // y, x % y) 형태의 튜플을 반환함examples = [45, 60, 125, 350, 601, 1234, 3599]for total_seconds in examples: minutes, seconds = divmod(total_seconds, 60) print(..
2025. 11. 12.
[Python] 초급 퀴즈27 - 2차원 배열
1. 다음 중 3행 3열의 2차원 배열을 올바르게 초기화하는 코드는?array = [0] * 3 * 3array = [[0] * 3 for _ in range(3)]array = [[0 for i in range(3)] * 3]array = [ [0, 0, 0], [0, 0], [0, 0, 0, 0] ] 2. 다음 코드의 출력 결과로 알맞은 것은?n = 2array = [[i * n + j for j in range(n)] for i in range(n)]print(array)[[0, 1], [2, 3]][[1, 2], [3, 4]][[0, 2], [1, 3]][[0, 1, 2], [3, 4, 5]] 3. 다음 코드에서 array[2][1]의 값은?array = [ [10, 20, 30], ..
2025. 4. 14.