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

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

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

השאלה

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

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

def f1(a, b, c):
    a0, a1, a2 = a[0], a[1], a[2]
    a0, a1, a2 = 3, 2, 1
    b+=a
    c[:3] = c[:2:-1]
    return b[6]

lst1 = [1, 2, 3]
lst2 = lst1[:]
lst3 = lst2 + lst1
lst2 = f1(lst1, lst2 + lst3, lst3)
print(lst1)
print(lst2)
print(lst3)

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

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

def func1(string):
    string.replace("abc","def")
    print(string)

def func2(string):
    func1(string)
    print(string)
    return string.startswith("def")

print(func2("abcdef"))

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

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

def different_cells_num(mat1, mat2, th):
    rows = len(mat1)
    cols = len(mat1[0])
    counter = 0
    for i in range(rows):
        for j in range(cols):
            if abs(mat2[i][j]-mat1[i][j]) > th:
                counter += 1
    return counter

mat_1 = [[120, 105, 34],[160, 200, 0]]
mat_2 = [[120, 100, 30],[150, 150, 132]]
print(different_cells_num(mat_1, mat_2, 0))
print(different_cells_num(mat_1, mat_2, 5))
print(different_cells_num(mat_1, mat_2, 10))

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

הפתרון המלא

סעיף א:

[1, 2, 3]
1
[3, 2, 1, 1, 2, 3]

סעיף ב:

abcdef
abcdef
False

סעיף ג:

5
3
2

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

שאלה 2 ←