콘텐츠로 이동

파이썬 객관식 심화 — 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 124 124 134 145 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 CB+ F CA+ F C+B+ F+ C+

3. (while 시뮬레이션)

balance = 1000
month = 0
while balance < 1500:
    balance = balance + balance // 10
    month += 1
print(month, balance)

4 14645 16105 14646 16104 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.0100 40 2090 40 18.0100 25 20.0100 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.05 1025.0 100.07.0 14.05.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 6the 3 9the 3 6the 2 6the 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'] 3Error