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

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

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

השאלה

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

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

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)
print(lst1)

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

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

def f1(str1):
    lst1 = str1.replace('t', 'T').split('o')
    print(lst1[0] + lst1[2])
    return "".join(lst1)

def f2(str2):
    str2 += str2
    for i, ch in enumerate(str2):
        if i % 2 == 0 and i < 5:
            print(str2[i:i+3], end='/')

str = 'hippopotamus'
f1(str)
print(str[::-1])
f2(str)

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

def copy_matrix_1():
     M = [[1,2,3], [4,5,6]]
     M_copy = M[:]
     M_copy[0] =  [0,0,0]
     print(M)

def copy_matrix_2(M):
     M_copy=[[M[i][j] for j in range(len(M[0]))] for i in range(len(M))]
     M_copy[1] =  [0,0,0]
     print(M)

def copy_matrix_3(M):
     M_copy = M
     M_copy[2] = [0,0,0]

copy_matrix_1()
M = [[10,20,30], [4,5,6], [7,8,9]]
copy_matrix_2(M)
copy_matrix_3(M)
print(M)

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

הפתרון המלא

סעיף א:

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

סעיף ב:

hippTamus
sumatopoppih
hip/ppo/opo/

סעיף ג:

[[1, 2, 3], [4, 5, 6]]
[[10, 20, 30], [4, 5, 6], [7, 8, 9]]
[[10, 20, 30], [4, 5, 6], [0, 0, 0]]

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

שאלה 2 ←