שאלות ממבחני עבר#
בחלק זה תתרגלו מספר שאלות OOP שנשאלו במבחני עבר. נסו לפתור אותן בעצמכם - מומלץ אפילו בדף ועט! לאחר מכן בדקו את תשובותיכם. קיימים גם פתרונות אפשריים שמוצעים לכם פה, אך מדובר בפתרון אחד אפשרי. אם הפתרון שלכם עובד, הוא יקבל את הנקודות.
תשפ”ב סמסטר ב’ מועד א’#
פתחו את המבחן 2223BmoedA וענו על כל שאלה 2.
פתרונות#
2.A
class Ingredient:
def __init__(self, name, calories, weight=100):
self.name = name
self.calories = calories
if type(weight) in [int, float] and weight > 0:
self.weight = weight
else:
self.weight = 100
print('weight error')
def calc_calories(self):
return int(self.weight / 100 * self.calories)
def __repr__(self):
return f"{self.name}: {self.calc_calories()} calories"
lettuce = Ingredient('Lettuce', 17, 32)
egg = Ingredient ('Egg', 60, 134)
mushroom = Ingredient ('Mushroom', 25, 22)
onion = Ingredient ('Onion', 25, 200)
print(lettuce.calc_calories())
print(egg)
print(mushroom)
print(onion)
lettuce = Ingredient('Lettuce', 17, -4)
lettuce = Ingredient('Lettuce', 17, 'X')
5
Egg: 80 calories
Mushroom: 5 calories
Onion: 50 calories
weight error
weight error
2.B
class Recipe:
def __init__(self, name, time, ing_lst):
self.name = name
self.time = time
if ing_lst:
self.ing_lst = ing_lst
else:
print('ingridients error')
def calc_calories(self):
return sum([amount * ing.calc_calories() for amount, ing in self.ing_lst])
def __repr__(self):
result = f"{self.name}, {self.time} minutes \n"
for amount, ing in self.ing_lst:
result += f"{ing} * {amount}\n"
result += f"total = {self.calc_calories()}"
return result
omelette = Recipe('Omelette', 5, [])
egg = Ingredient ('Egg', 60, 134)
mushroom = Ingredient ('Mushroom', 25, 22)
omelette = Recipe('Omelette', 5, [(2,egg), (4, mushroom)])
print(omelette)
print(omelette.calc_calories())
ingridients error
Omelette, 5 minutes
Egg: 80 calories * 2
Mushroom: 5 calories * 4
total = 180
180
2.C
def add_ingredient(self, amount,ingredient):
old_amount = 0
for x in self.ing_lst:
if x[1].name == ingredient.name:
old_amount = x[0]
self.ing_lst.remove(x)
break
self.ing_lst.append((amount + old_amount, ingredient))
# biolerplte code for adding a new method to a class
Recipe.add_ingredient=add_ingredient
omelette = Recipe('Omelette', 5, [(2,egg)])
print(omelette)
omelette.add_ingredient(2,mushroom)
print(omelette)
omelette.add_ingredient(1,egg)
print(omelette)
Omelette, 5 minutes
Egg: 80 calories * 2
total = 160
Omelette, 5 minutes
Egg: 80 calories * 2
Mushroom: 5 calories * 2
total = 170
Omelette, 5 minutes
Mushroom: 5 calories * 2
Egg: 80 calories * 3
total = 250
2.D
def sort_ing(self):
self.ing_lst.sort(key=lambda x: x[0] * x[1].weight)
# biolerplte code for adding a new method to a class
Recipe.sort_ing=sort_ing
# Note that other values are presented in the example
salad = Recipe('Salad', 5, [(2, onion), (4, mushroom), (2, lettuce)])
print(salad)
salad.sort_ing()
print(salad)
Salad, 5 minutes
Onion: 50 calories * 2
Mushroom: 5 calories * 4
Lettuce: 17 calories * 2
total = 154
Salad, 5 minutes
Mushroom: 5 calories * 4
Lettuce: 17 calories * 2
Onion: 50 calories * 2
total = 154
2.E
def __lt__(self, other):
return (self.time, self.calc_calories()) < (other.time, other.calc_calories())
Recipe.__lt__=__lt__
salad_with_egg = Recipe('Salad with egg', 10, [(2, onion), (1, mushroom), (2, lettuce)])
print(salad)
print(salad_with_egg)
print(omelette)
print(salad_with_egg < salad)
print(salad < omelette)
Salad, 5 minutes
Mushroom: 5 calories * 4
Lettuce: 17 calories * 2
Onion: 50 calories * 2
total = 154
Salad with egg, 10 minutes
Onion: 50 calories * 2
Mushroom: 5 calories * 1
Lettuce: 17 calories * 2
total = 139
Omelette, 5 minutes
Mushroom: 5 calories * 2
Egg: 80 calories * 3
total = 250
False
True
תשפ”ב סמסטר ב’ מועד ב’#
פתחו את המבחן 2223BmoedB וענו על כל שאלה 2.
פתרונות#
2.A
class Client:
def __init__(self, name, salary, balance):
self.name = name
self.salary = salary
self.balance = balance
if type(name) != str:
self.name = 'Error'
print('Name Error')
def salary_raise(self, amount):
self.salary += amount
self.balance += 0.3 * amount
def __repr__(self):
return f'Name: {self.name}, Salary: {self.salary}, Balance:{self.balance}'
yarin = Client('Yarin', 10000, 20000)
err = Client(509,3000,10000)
print(err)
yarin.salary_raise(1000)
print(yarin)
Name Error
Name: Error, Salary: 3000, Balance:10000
Name: Yarin, Salary: 11000, Balance:20300.0
2.B
class Bank:
def __init__(self, name):
self.name = name
self.client_lst = []
self.total_balance = 0
def add_client(self, client):
self.client_lst.append(client)
self.total_balance += client.balance
def __repr__(self):
bank_str = f'Name: {self.name}, Balance: {self.total_balance}\n'
bank_str += f'Bank clients:\n'
for client in self.client_lst:
bank_str += str(client)
bank_str += f'\nClient balance percent: {int(100 * client.balance / self.total_balance)}\n'
return bank_str
bank = Bank('Python Bank')
yarin = Client('Yarin', 10000, 20000)
jonathan = Client('Jonathan', 5000, 10000)
bank.add_client(yarin)
bank.add_client(jonathan)
print(bank)
Name: Python Bank, Balance: 30000
Bank clients:
Name: Yarin, Salary: 10000, Balance:20000
Client balance percent: 66
Name: Jonathan, Salary: 5000, Balance:10000
Client balance percent: 33
2.C
def mul_crit(client,alpha,beta):
return alpha + 2000 * beta / client.balance
yarin = Client('Yarin', 10000, 50000)
mul_crit(yarin,5000,100000)
9000.0
2.D
def max_salary(bank):
return max(bank.client_lst, key=lambda x: x.salary).salary
bank = Bank('Python Bank')
yarin = Client('Yarin', 10000, 20000)
jonathan = Client('Jonathan', 5000, 10000)
bank.add_client(yarin)
bank.add_client(jonathan)
max_salary(bank)
10000
2.E
def approved_clients(bank):
return sorted(bank.client_lst, key=lambda x: \
mul_crit(x, max(bank.client_lst, key=lambda x: x.salary).salary \
- x.salary, bank.total_balance))[:len(bank.client_lst) // 2]