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
في هذا المثال نعرف نقطتين ثم نحسب المسافة بينهما. والمسافة الإقليدية بين نقطتين \((x_1, y_1)\) و \((x_2, y_2)\) تتبع معادلة فيثاغورس:
\[ \text{distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \]
وتذكر أن:
\[ \sqrt{x} = x^{1/2} \]
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 split(data: list, ratio: float) -> tuple[list, list]:
= int(len(data) * ratio)
idx return data[:idx], data[idx:]
نختبر الإجراء ونلاحظ أن الناتج من نوع صف (tuple
). فنستعمل التعيين المتعدد لاستخراج القيم من الصف:
= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
xs = split(xs, 0.80)
a, b
assert a == [10, 20, 30, 40, 50, 60, 70, 80]
assert b == [90, 100]
نريد أن نعرف إجراء سحب الرصيد لأي مستخدم ولأي جهاز صراف آلي ولأي مبلغ:
def withdraw_cash(balance, amount, atm_cash):
if balance >= amount:
if atm_cash >= amount:
-= amount
balance -= amount
atm_cash print("Withdrawal successful!")
else:
print("ATM does not have enough cash.")
else:
print("Insufficient balance.")
500, 200, 1000)
withdraw_cash(500, 200, 100)
withdraw_cash(500, 600, 1000) withdraw_cash(
Withdrawal successful!
ATM does not have enough cash.
Insufficient balance.
في هذا المثال نوضح استعمال مؤشرين على نفس النص لمعرفة ما إذا كانت الكلمة متناظرة:
i
يبدأ من أول حرف وينتهي عند المنتصفj
يبدأ من آخر حرف وينتهي عند المنتصفdef is_palindrome(word):
"""
A palindrome word is one that can be read the same way from both ends.
"""
# إزالة الاختلافات في الحروف الكبيرة والصغيرة
= word.lower()
word
# إزالة الفواصل (لتطابق الكلمات مثل: "Race car")
= word.replace(" ", "")
word
# التحقق من أن الكلمة متناظرة
for i in range(len(word) // 2):
= len(word) - i - 1
j if word[i] != word[j]:
return False
return True
# الاختبارات
assert is_palindrome("radar")
assert is_palindrome("level")
assert is_palindrome("madam")
assert is_palindrome("توت")
assert is_palindrome("خوخ")
assert is_palindrome("Race car")
assert is_palindrome("حصان ناصح")
assert not is_palindrome("python")
بدل الحلقة، وكان لنا أن نكتب باختصار: word == word[::-1]
وهي تعني أن النص متناظر إذا كان مساوياً لنفسه بالمقلوب.
def is_palindrome(word):
= word.lower()
word = word.replace(" ", "")
word return word == word[::-1]
اكتب فعلًا يصحح الإخطاء الشائعة في الإملاء العربي.
والمطلوب فقط تصحيح الأخطاء المذكورة.
= {
corrections_map 'مكتبه': 'مكتبة',
'لعبه': 'لعبة',
'روايه': 'رواية',
'ظابط': 'ضابط',
'قرظ': 'قرض',
'ضهر': 'ظهر',
'أحسنتي': 'أحسنت',
'رأيتكي': 'رأيتك',
}
def correct_spelling(sentence: str) -> str:
= sentence.split()
words = []
result for word in words:
= word.startswith('ال')
al if al:
= word[2:]
word = corrections_map.get(word, word)
correction if al:
= 'ال' + correction
correction
result.append(correction)return ' '.join(result)
assert (
'المكتبه فيها الروايه التي أبحث عنها') ==
correct_spelling('المكتبة فيها الرواية التي أبحث عنها'
)
assert (
'أعطاك الظابط القرظ بعد الضهر') ==
correct_spelling('أعطاك الضابط القرض بعد الظهر'
)
assert (
'رأيتكي أحسنتي') == 'رأيتك أحسنت') correct_spelling(
ماذا لو أردت أن تضم قائمة من الأشخاص بحيث يكون فارق العمر بينهم أقل ما يمكن؟
Ahmad: 24
Mohannad: 17
Mohammed: 16
Salem: 32
Ali: 26
Samir: 31
أولاً سنرتب القائمة بناءً على العمر، ثم نأخذ العناصر اثنين اثنين، وبذلك نكون حققنا الهدف.
def make_pairs_by_age_diff(items: list[tuple[str, int]]) -> list[tuple[str, str]]:
"""Make pairs of people by their age difference minimum."""
= [age for name, age in items]
ages
ages.sort()= []
items_sorted for age in ages:
for i in items:
if i[1] == age:
items_sorted.append(i)= []
pairs for i in range(len(items_sorted) - 1):
+1]))
pairs.append((items_sorted[i], items_sorted[ireturn pairs
= [
peers "Ahmad", 24),
("Mohannad", 17),
("Mohammed", 16),
("Salem", 32),
("Ali", 26),
("Samir", 31),
( ]
الاختبارات
= make_pairs_by_age_diff(peers)
pairs for p in pairs:
if "Ahmad" in p:
assert "Ali" in p
if "Mohannad" in p:
assert "Mohammed" in p
if "Salem" in p:
assert "Samir" in p
مسألة التوفيق بين مجموعتين تجدها في سياقات كثيرة.
نريد في هذا التمرين التوفيق بين أشخاص وما لديهم من مهارات وبين شركات وما تطلبها من هذه المهارات.
يمكن أن تبدأ بهذا الشكل لإيجاد كمية التوافق:
= {"A", "X", "Y"}
have = {"A", "B", "C", "D"}
want print("نسبة الإعجاب:", len(set.intersection(have, want)) / len(want))
نسبة الإعجاب: 0.25
وفي العادة تكون البيانات مضمنة في قائمة على النحو التالي:
= [
haves
{"name": "Ahmad",
"skills": [
"Python",
"Go",
"JavaScript",
],
},
{"name": "Jawad",
"skills": [
"Negotiation",
"Communication",
"Business",
"Marketing",
],
},
]
= [
wants
{"job": "Data Scientist",
"requirements": [
"Python",
"Machine Learning",
"Statistics",
],
},
{"job": "Sales Manager",
"requirements": [
"Negotiation",
"Communication",
],
}, ]
نعالجها لكل عنصر في قائمة wants
:
= {}
ratios for want in wants:
for have in haves:
= len(set.intersection(set(want["requirements"]), set(have["skills"]))) / len(want["requirements"])
ratio if want['job'] not in ratios:
'job']] = {}
ratios[want['job']][have['name']] = ratio
ratios[want[ ratios
{'Data Scientist': {'Ahmad': 0.3333333333333333, 'Jawad': 0.0},
'Sales Manager': {'Ahmad': 0.0, 'Jawad': 1.0}}
الآن نرتب كل وظيفة بحسب الأعلى توافقًا.
أولاً نكتب دالة ترتِّب قاموسًا، ثم سنستعملها:
def sort_nested_dict(d: dict) -> dict:
"""Sorts a dictionary by values"""
= {}
result for key in d.keys():
= {}
result[key] = [v for v in d[key].values()]
values =True)
values.sort(reversefor v1 in values:
for k, v2 in d[key].items():
if v2 == v1:
= v2
result[key][k] return result
= sort_nested_dict(ratios)
sorted_ratios sorted_ratios
{'Data Scientist': {'Ahmad': 0.3333333333333333, 'Jawad': 0.0},
'Sales Manager': {'Jawad': 1.0, 'Ahmad': 0.0}}
ولكثرة الحاجة لترتيب البيانات، فإن إجراء sorted
متعدد الاستعمالات في بايثون. انظر Sorting Basics و Key Functions من مرجع بايثون تحت عنوان: “HOW TO”.
for k, v in ratios.items():
= sorted(v.items(), key=lambda x: x[1], reverse=True)
sorted_ratios[k] sorted_ratios
{'Data Scientist': [('Ahmad', 0.3333333333333333), ('Jawad', 0.0)],
'Sales Manager': [('Jawad', 1.0), ('Ahmad', 0.0)]}
فأول المرشحين في علوم البيانات هو:
"Data Scientist"][0][0] sorted_ratios[
'Ahmad'
وأول المرشحين في المبيعات هو:
"Sales Manager"][0][0] sorted_ratios[
'Jawad'