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

נוסח השאלה כטקסט
א. מה יודפס כאשר תרוץ התוכנית הבאה?
def fun1(lst1, lst2):
lst1 += lst2
lst1.append(100)
lst1 = lst1[-2:-len(lst1):-2]
print(lst1)
def fun2(lst):
return lst * 2
def fun3(lst2):
if lst2:
lst2 = fun2(lst2)
lst2 = sorted(lst2)
else:
lst2 = lst2 + [0]
fun2(lst2)
return lst2
my_lst1 = [1, 2, 3]
my_lst2 = [-3, -2, -1]
fun1(my_lst1, my_lst2)
print(my_lst1)
fun3(my_lst2)
print(my_lst2)אין צורך בהסבר.
ב. מה יודפס כאשר תרוץ התוכנית הבאה?
def func1(c):
u2l = (ord("A") <= ord(c) <= ord("Z"))*(ord("f") - ord("F"))
l2u = (ord("a") <= ord(c) <= ord("z"))*(ord("R") - ord("r"))
return chr(ord(c) + u2l + l2u)
def func2(s):
c1 = s[0]
c2 = s[-1]
c3 = s[1:-1]
return func1(c1) + c3 + func1(c2)
def func3(s1, s2):
s1 += s2
s1.upper()
return s1.replace("old", "new")
a1 = "Boom"
print(func2(a1))
print(a1[::-3])
a3 = "Print is the old debugger!"
a4 = "\nAre you sure?"
a3 = func3(a3, a4)
print(a3)אין צורך בהסבר.
ג. מה יודפס כאשר תרוץ התוכנית הבאה?
import pandas as pd
import matplotlib.pyplot as plt
def analyze_df(file_path):
df = pd.read_csv(file_path)
ages = df["Age"].tolist()
print(f"{min(ages):.2f}", max(ages))
names = df["Name"].tolist()
plt.plot(ages, names)
plt.show()
analyze_df("data.csv")file data.csv
| Name | Age | Grade |
|---|---|---|
| Abby | 1 | 90 |
| Max | 3 | 100 |
| George | 2 | 32 |
| Elen | 4 | 99 |
1) מה יודפס?
2) יש להציג גרף בצורה ברורה ומדויקת ככל האפשר, כולל הערכים בשנתות של ציר X וציר Y.
הפתרון המלא
סעיף א:
[-1, -3, 2]
[1, 2, 3, -3, -2, -1, 100]
[-3, -2, -1]סעיף ב:
booM
mB
Print is the new debugger!
Are you sure?סעיף ג:
1.00 4