파이썬 서술형 — SET 16¶
이름: ____________ 점수: _____ / 100
출력형은 실행 결과와 그렇게 되는 이유를 함께 서술하세요. 작성형은 실행 가능한 코드로 답하세요. (범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)
[Ⅰ. 출력 예측 + 이유 서술] (각 10점)¶
1.¶
def f(a, b=[]):
b.append(a)
return b
x = f(1)
y = f(2)
print(x, y, x is y)
출력: ________________
이유:
2.¶
class Counter:
count = 0
def __init__(self):
Counter.count += 1
self.id = Counter.count
c1 = Counter()
c2 = Counter()
c3 = Counter()
print(c1.id, c2.id, c3.id, Counter.count)
출력: ________________
이유:
3.¶
d1 = {"a": 1, "b": 2}
d2 = d1
d3 = d1.copy()
d2["a"] = 100
d3["b"] = 200
print(d1, d2, d3)
출력: ________________
이유:
4.¶
import random
random.seed(1)
nums = [random.randint(1, 10) for _ in range(5)]
print(len(nums), all(1 <= n <= 10 for n in nums))
출력: ________________
이유:
[Ⅱ. 코드 작성] (각 15점)¶
5. (리스트) 순서를 유지하면서 중복을 제거하는 함수 dedup(lst)를 작성하세요.¶
dedup([1, 2, 2, 3, 1, 4]) → [1, 2, 3, 4]
6. (튜플) 숫자 리스트를 받아 (최솟값, 최댓값, 평균)을 튜플로 반환하는 함수 stats(nums)를 작성하세요.¶
stats([3, 1, 4, 1, 5]) → (1, 5, 2.8)
7. (set) 두 리스트의 공통 원소를 정렬된 리스트로 반환하는 함수 common(a, b)를 작성하세요.¶
common([1, 2, 3, 4], [3, 4, 5, 6]) → [3, 4]
[Ⅲ. 오류 찾기·수정] (15점)¶
8.¶
class Stack:
def __init__(self):
items = []
def push(self, x):
self.items.append(x)
def pop(self):
return self.items.pop()
s = Stack()
s.push(1)
이 코드를 실행하면 어떤 문제가 발생하는지 설명하고, 올바르게 고친 코드를 작성하세요.