מבוא למחשב, שפת פייתוןלתרגל עם המורה AI

מבוא למחשב פייתון (02340128), חורף תשפ״ו · מועד א׳ · פתרונות מלאים

שאלות: 1 · 2 · 3 · 4

שאלה 1 · 25 נקודות

מעקב: רשימות, מטריצות וגרף עמודות

שאלה 1
הפתרון המלא

סעיף א:

[25, 4, 36, 9]
[5, 0, 6, 3, 7]
python

סעיף ב:

[[0, 2], [1, 3]]
[['a', 'BAMMM!'], ['c', 'd']]
True

סעיף ג:

[2, 3, 0, 5, 0, 7]

(הגרף בתשובה הרשמית: גרף עמודות כחולות: ציר X עם שנתות 0 עד 5, ציר Y עם שנתות 0 עד 7 בקפיצות של 1. גבהי העמודות: 0→2, 1→3, 2→0, 3→5, 4→0, 5→7.)

שאלה 2 · 25 נקודות

מדינות "זוגיות-חזקות" לפי ערכי Unicode

שאלה 2
הפתרון המלא

סעיף א:

def check_even_strong(s):
    sum_even = sum([ord(c) for c in s[::2]])
    sum_odd = sum([ord(c) for c in s[1::2]])
    return sum_even>sum_odd


def find_even_strong_countries(countries):
    return [country for country in countries if check_even_strong(country)]

# פתרון נוסף

def find_even_strong_countries(countries):
    even_strong_list = []
    for name in countries:
        even_sum = 0; odd_sum = 0
        for i in range(len(name)):
            if i % 2 == 0:
                even_sum += ord(name[i])
            else:
                odd_sum += ord(name[i])
        if even_sum > odd_sum:
            even_strong_list.append(name)
    return even_strong_list

סעיף ב:

def find_best_friend(friends_lists):
    max_count = -1
    best_friend_idx = 0
    for idx, country_list in enumerate(friends_lists):
        # using the function from section (a)
        even_strong_countries= find_even_strong_countries(country_list)
        current_count = len(even_strong_countries)
        if current_count > max_count:
            max_count = current_count
            best_friend_idx = idx
    return best_friend_idx

סעיף ג:

(ג-1) O(n*m) כאשר

n — מספר המדינות ברשימה countries

m — האורך המקסימלי של שם מדינה

(ג-2) O(k*n*m) כאשר

k — מספר החברים ברשימה friends_lists

n — המספר המקסימלי של מדינות ברשימה של חבר

m — האורך המקסימלי של שם מדינה

שאלה 3 · 25 נקודות

קיבולת משאית מינימלית בחיפוש בינארי

שאלה 3
הפתרון המלא

סעיף א:

def can_ship(weights, days, capacity):
    total = 0
    needed_days = 1
    for w in weights:
        if w > capacity:
            return False
        if total + w > capacity:
            needed_days += 1
            total = w
        else:
            total += w
    return needed_days <= days

סעיף ב:

def min_capacity(weights, days):
    low, high = max(weights), sum(weights)
    while low < high:
        mid = (low + high) // 2
        if can_ship(weights, days, mid): # True, needed_days <= days
            high = mid - 1
        else:
            low = mid + 1
    return low

שאלה 4 · 25 נקודות

איזון מאזניים במשקולות ברקורסיה

שאלה 4
הפתרון המלא

התשובה:

def is_balanced(weights, target):
    # Base Case: No more weights to place
    if len(weights) == 0:
        return target == 0
    # Option 1: Put weight on the RIGHT (target decreases)
    right = is_balanced(weights[1:], target - weights[0])
    # Option 2: Put weight on the LEFT (target increases)
    left = is_balanced(weights[1:], target + weights[0])
    # Option 3: Don't use the weight
    skip = is_balanced(weights[1:], target)
    return right or left or skip

לתרגל שאלות כאלה עם המורה AI