파이썬 객관식 심화 — SET 11 (긴 코드)¶
이름: ____________ 점수: _____ / 100 난이도: ★★★★★
다소 긴 프로그램입니다. 차분히 끝까지 추적하여 실행 결과를 고르세요. (문항당 10점, 범위: 1~8장 종합)
1. (문자열 처리)¶
text = "Hello World Python"
vowels = "aeiouAEIOU"
count = 0
result = ""
for ch in text:
if ch in vowels:
count += 1
elif ch != " ":
result += ch
print(count, len(result))
① 5 12 ② 4 12 ③ 4 13 ④ 4 14 ⑤ 5 13
2. (조건 분기)¶
def grade(score, attendance):
if attendance < 70:
return "F"
if score >= 90:
base = "A"
elif score >= 80:
base = "B"
elif score >= 70:
base = "C"
else:
base = "F"
if base != "F" and attendance >= 95:
return base + "+"
return base
print(grade(85, 100), grade(95, 60), grade(75, 96))
① B+ F C+ ② B F C ③ B+ F C ④ A+ F C+ ⑤ B+ F+ C+
3. (while 시뮬레이션)¶
balance = 1000
month = 0
while balance < 1500:
balance = balance + balance // 10
month += 1
print(month, balance)
① 4 1464 ② 5 1610 ③ 5 1464 ④ 6 1610 ⑤ 4 1610
4. (중첩 for 정렬)¶
nums = [5, 2, 8, 1, 9, 3]
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[j] < nums[i]:
nums[i], nums[j] = nums[j], nums[i]
print(nums)
① [9, 8, 5, 3, 2, 1] ② [5, 2, 8, 1, 9, 3] ③ [1, 2, 3, 5, 8, 9] ④ [1, 2, 8, 5, 9, 3] ⑤ [2, 1, 5, 3, 9, 8]
5. (함수·다중 반환)¶
def analyze(nums):
total = 0
largest = nums[0]
for n in nums:
total += n
if n > largest:
largest = n
return total, largest, total / len(nums)
s, m, avg = analyze([10, 25, 5, 40, 20])
print(s, m, avg)
① 100 40 20.0 ② 100 40 20 ③ 90 40 18.0 ④ 100 25 20.0 ⑤ 100 40 25.0
6. (클래스·상태 관리)¶
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
self.history = []
def deposit(self, amount):
self.balance += amount
self.history.append(amount)
def withdraw(self, amount):
if amount > self.balance:
self.history.append(0)
else:
self.balance -= amount
self.history.append(-amount)
acc = BankAccount(100)
acc.deposit(50)
acc.withdraw(200)
acc.withdraw(80)
print(acc.balance, acc.history)
① 70 [50, 0, -80] ② 70 [50, -200, -80] ③ -130 [50, -200, -80] ④ 70 [50, 0, 80] ⑤ 150 [50, 0, -80]
7. (예외 처리·집계)¶
def parse_total(items):
total = 0
errors = 0
for x in items:
try:
total += int(x)
except ValueError:
errors += 1
return total, errors
print(parse_total(["10", "5", "abc", "3", "x", "7"]))
① (25, 0) ② (25, 2) ③ (18, 2) ④ (25, 4) ⑤ Error
8. (모듈·math)¶
import math
def hypotenuse(a, b):
return math.sqrt(a ** 2 + b ** 2)
print(hypotenuse(3, 4), hypotenuse(6, 8))
① 5.0 10.0 ② 5 10 ③ 25.0 100.0 ④ 7.0 14.0 ⑤ 5.0 10
9. (리스트·딕셔너리)¶
sentence = "the cat sat on the mat the cat ran"
words = sentence.split()
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
best = ""
best_count = 0
for w in freq:
if freq[w] > best_count:
best_count = freq[w]
best = w
print(best, best_count, len(freq))
① cat 2 6 ② the 3 9 ③ the 3 6 ④ the 2 6 ⑤ the 3 7
10. (종합)¶
students = [
{"name": "Kim", "scores": [80, 90, 70]},
{"name": "Lee", "scores": [60, 60, 90]},
{"name": "Park", "scores": [100, 95, 90]},
]
passed = []
for s in students:
avg = sum(s["scores"]) / len(s["scores"])
if avg >= 75:
passed.append(s["name"])
print(passed, len(passed))
① ['Kim', 'Lee', 'Park'] 3 ② ['Kim', 'Park'] 2 ③ ['Park'] 1 ④ ['Kim', 'Park'] 3 ⑤ Error