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

מבוא למחשב פייתון (02340128) אביב תשפ״ה · מועד ב׳, שאלה 1: פתרון

מעקב: מחרוזות, רשימות ומטריצות25 נקודות

השאלה

מבוא למחשב פייתון (02340128) אביב תשפ״ה · מועד ב׳, שאלה 1: פתרון
נוסח השאלה כטקסט

א. מה יודפס כאשר תרוץ התוכנית הבאה?

def func1(x, a, b):
    x = x.replace(a, b).split(":")
    return x[::-1]

def func2(x, y):
    all = x+y
    all[::2] = x
    all[1::2] = y
    return "".join(all)

my_note = "Elsa, the balloon."
x = "o:$:i:t:L"
y = "!:g:t:?:e"
x = func1(x, "$", "-")
print(x)
y = func1(y, "?", "-")
print(y)
print(my_note, func2(x, y))

אין צורך בהסבר.

ב. מה יודפס כאשר תרוץ התוכנית הבאה?

def mystery_func(lst, shift, increment):
    new_lst = [x + increment for x in lst if type(x) == int]
    if not new_lst:
        return []
    new_lst = new_lst[shift:] + new_lst[:shift]
    new_lst[0] = new_lst[0] * 2
    return new_lst

lst1 = [1, 2, 3, 4]
print(mystery_func(lst1, 2, 5))
lst2 = [10, "x", 15, 20]
new_lst2 = mystery_func(lst2, 1, 3)
print(new_lst2)
lst3 = [0, 1, 2, 3, 4]
new_lst3 = mystery_func(lst3, 4, 8)
print(lst3)

אין צורך בהסבר.

ג. מה יודפס כאשר תרוץ התוכנית הבאה?

def matrix_ops(matrix):
    temp = matrix
    matrix[0] = [10, 20]
    temp = [row[:] for row in matrix]
    temp[1][0] = 99
    matrix[0:2] = [[1, 2], [3, 4]]
    print(temp)
    matrix = [[5, 6], [7, 8]]
    temp[0], temp[1] = temp[1], temp[0]
    return matrix

data = [[1, 2], [3, 4], [5, 6]]
result = matrix_ops(data)
print(data)

אין צורך בהסבר.

הפתרון המלא

סעיף א:

['L', 't', 'i', '-', 'o']
['e', '-', 't', 'g', '!']
Elsa, the balloon. Let-it-go!

סעיף ב:

[16, 9, 6, 7]
[36, 23, 13]
[0, 1, 2, 3, 4]

סעיף ג:

[[10, 20], [99, 4], [5, 6]]
[[1, 2], [3, 4], [5, 6]]

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

שאלה 2 ←