def wage(hours: float, per_hour_rate: float) -> float:
return hours * per_hour_rate
print(wage(10, 50))
print(wage(3.5, 120))500
420.0
عدد المسائل: 8 الوقت التقديري: 45–60 دقيقة
ترتيب الحلول يطابق مسائل مسائل الدوال من الأسهل إلى الأصعب.
الفكرة: ضرب عدد الساعات في سعر الساعة.
def wage(hours: float, per_hour_rate: float) -> float:
return hours * per_hour_rate
print(wage(10, 50))
print(wage(3.5, 120))500
420.0
الفكرة: إضافة فرق السنوات إلى العمر الحالي.
def age_at(age_now: int, current_year: int, at_year: int) -> int:
return age_now + (at_year - current_year)
print(age_at(0, 2000, 2010)) # بعد الولادة
print(age_at(20, 2000, 2010)) # في المستقبل
print(age_at(20, 2000, 1995)) # في الماضي10
30
15
الفكرة: 40 - len(text) مسافة ثم النص.
def print_right(text: str) -> None:
text_length = len(text)
spaces_amount = 40 - text_length
print(' ' * spaces_amount + text)
print_right('Monty')
print_right("Python's")
print_right('Flying Circus') Monty
Python's
Flying Circus
الفكرة: حلقة for وrange(1, height + 1).
def triangle(letter: str, height: int) -> None:
for i in range(1, height + 1):
print(letter * i)
triangle('L', 10)L
LL
LLL
LLLL
LLLLL
LLLLLL
LLLLLLL
LLLLLLLL
LLLLLLLLL
LLLLLLLLLL
الفكرة: صفٌّ خارجي للارتفاع، وكل صف يكرّر الحرف بعرض width.
def rectangle(letter: str, width: int, height: int) -> None:
for _ in range(height):
print(letter * width)
rectangle('X', width=5, height=2)XXXXX
XXXXX
الفكرة: أربعة أسطر ثابتة الشكل مع استخدام number وnumber - 1.
def tea_verse(number: int) -> None:
print(number, 'cups of tea on the tray')
print(number, 'cups of tea')
print('Pour one out, pass it around')
print(number - 1, 'cups of tea on the tray')
tea_verse(99)99 cups of tea on the tray
99 cups of tea
Pour one out, pass it around
98 cups of tea on the tray
مثال: طباعة عدة أبيات متتابعة:
for n in range(5, 0, -1):
tea_verse(n)
print()5 cups of tea on the tray
5 cups of tea
Pour one out, pass it around
4 cups of tea on the tray
4 cups of tea on the tray
4 cups of tea
Pour one out, pass it around
3 cups of tea on the tray
3 cups of tea on the tray
3 cups of tea
Pour one out, pass it around
2 cups of tea on the tray
2 cups of tea on the tray
2 cups of tea
Pour one out, pass it around
1 cups of tea on the tray
1 cups of tea on the tray
1 cups of tea
Pour one out, pass it around
0 cups of tea on the tray
الفكرة: تطبيق صيغة فيثاغورس.
def euclidean_distance(x1: float, y1: float, x2: float, y2: float) -> float:
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
print(euclidean_distance(x1=0, y1=0, x2=3, y2=4))
print(euclidean_distance(1, 1, -2, -2))5.0
4.242640687119285
الفكرة: شرط متداخل — الرصيد أولًا، ثم توفر النقود في الصراف.
def withdraw_cash(balance: float, amount: float, atm_cash: float) -> None:
if balance >= amount:
if atm_cash >= amount:
print('Withdrawal successful!')
else:
print('ATM does not have enough cash.')
else:
print('Insufficient balance.')
withdraw_cash(500, 200, 1000)
withdraw_cash(500, 200, 100)
withdraw_cash(500, 600, 1000)Withdrawal successful!
ATM does not have enough cash.
Insufficient balance.