{ "cells": [ { "cell_type": "raw", "id": "2a7da4a4", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "---\n", "title: \"مسائل الدوال\"\n", "og:title: functions_problems\n", "toc: true\n", "format:\n", " html: default\n", "---\n" ] }, { "cell_type": "markdown", "id": "c00a7858", "metadata": {}, "source": [ "**عدد المسائل:** 8  **الوقت التقديري:** 45–60 دقيقة\n", "\n", "تم ترتيب المسائل من الأسهل إلى الأصعب. اكتب الحل في الخلايا البرمجية أدناه.\n" ] }, { "cell_type": "markdown", "id": "ee788296", "metadata": {}, "source": [ "[تحميل الدفتر](https://raw.githubusercontent.com/HassanAlgoz/python/main/book/chapters/ch3-functions/exercise_3a.ipynb)" ] }, { "cell_type": "markdown", "id": "6c5f173e", "metadata": {}, "source": [ "## حساب الأجر\n", "\n", "عرِّف الدالة `wage` التي تحسب الأجر من عاملين:\n", "\n", "- `hours: float`: عدد الساعات\n", "- `per_hour_rate: float`: سعر الساعة الواحدة\n", "\n", "اجعل نوع العائد `float` باستعمال `-> float`.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d0bc3ea3", "metadata": {}, "outputs": [], "source": [ "def wage(hours: float, per_hour_rate: float) -> float:\n", " pass # اكتب هنا\n", "\n", "# أمثلة (غيّر القيم لتجرب):\n", "print(wage(10, 50))\n", "print(wage(3.5, 120))\n" ] }, { "cell_type": "markdown", "id": "bbf23bfe", "metadata": {}, "source": [ "## حساب العمر\n", "\n", "اكتب دالة `age_at` تحسب عمر الشخص في سنة معيّنة، معطى عمره الحالي والسنة الحالية والسنة المطلوبة.\n", "\n", "- `age_now: int` — العمر الآن\n", "- `current_year: int` — السنة الحالية\n", "- `at_year: int` — السنة التي تريد حساب العمر فيها\n", "\n", "**تلميح:** الفرق بين السنتين يُضاف إلى العمر الحالي (أو يُطرح إذا كانت السنة المطلوبة في الماضي).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fbf5f985", "metadata": {}, "outputs": [], "source": [ "def age_at(age_now: int, current_year: int, at_year: int) -> int:\n", " pass # اكتب هنا\n", "\n", "print(age_at(0, 2000, 2010)) # بعد الولادة\n", "print(age_at(20, 2000, 2010)) # في المستقبل\n", "print(age_at(20, 2000, 1995)) # في الماضي\n" ] }, { "cell_type": "markdown", "id": "f8ac6bfe", "metadata": {}, "source": [ "## المحاذاة إلى اليمين\n", "\n", "**المطلوب:** اكتب الدالة `print_right(text)` بحيث يظهر آخر حرف من `text` في **العمود 40** (كأن السطر مكوّنًا من 40 عمودًا).\n", "\n", "**تلميح:** استخدم `len`، وجمع النصوص (`+`)، وتكرار النص (`*`).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3b2136af", "metadata": {}, "outputs": [], "source": [ "def print_right(text: str) -> None:\n", " pass # اكتب هنا\n", "\n", "print_right('Monty')\n", "print_right(\"Python's\")\n", "print_right('Flying Circus')\n" ] }, { "cell_type": "markdown", "id": "6ee52825", "metadata": {}, "source": [ "## هرم\n", "\n", "**المطلوب:** اكتب الدالة `triangle(letter, height)` التي تطبع هرمًا من `height` مستويات باستخدام `letter`، بحيث يزداد طول كل صف حتى آخر مستوى.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c860b1dd", "metadata": {}, "outputs": [], "source": [ "def triangle(letter: str, height: int) -> None:\n", " pass # اكتب هنا\n", "\n", "triangle('L', 10)\n" ] }, { "cell_type": "markdown", "id": "29ea390e", "metadata": {}, "source": [ "## مستطيل\n", "\n", "**المطلوب:** اكتب الدالة `rectangle(letter, width, height)` التي تطبع مستطيلًا بعرض `width` وارتفاع `height` من `letter`.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8c160ce8", "metadata": {}, "outputs": [], "source": [ "def rectangle(letter: str, width: int, height: int) -> None:\n", " pass # اكتب هنا\n", "\n", "rectangle('X', width=5, height=2)\n" ] }, { "cell_type": "markdown", "id": "a7300d02", "metadata": {}, "source": [ "## أبيات الشاي\n", "\n", "**المطلوب:** اكتب الدالة `tea_verse(number)` لتطبع **أربعة أسطر** من البيت، يبدأ بعدد `number` كوبًا وينتهي بـ `number - 1` كوبًا (وفق النموذج في المثال التوضيحي بعد الحل).\n", "\n", "**فكرة السطرين الأوسطين:** مثلاً «Pour one out, pass it around».\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3c922c95", "metadata": {}, "outputs": [], "source": [ "def tea_verse(number: int) -> None:\n", " pass # اكتب هنا\n", "\n", "tea_verse(99)\n" ] }, { "cell_type": "markdown", "id": "07d9b1a9", "metadata": {}, "source": [ "إذا أردت طباعة الأغنية عدة مرات، يمكنك استخدام حلقة `for` مع `range` (اختياري).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "defbfcff", "metadata": {}, "outputs": [], "source": [ "# اختياري — لا يلزم لإكمال المسألة:\n", "# for n in range(5, 0, -1):\n", "# tea_verse(n)\n", "# print()\n" ] }, { "cell_type": "markdown", "id": "eebf833c", "metadata": {}, "source": [ "## طول الخط المستقيم بين نقطتين\n", "\n", "**المسافة الإقليدية** بين نقطتين $(x_1, y_1)$ و $(x_2, y_2)$:\n", "\n", "$$\n", "\\text{distance} = \\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\n", "$$\n", "\n", "وتذكّر أن $\\sqrt{x} = x^{1/2}$.\n", "\n", "**المطلوب:** الدالة `euclidean_distance(x1, y1, x2, y2) -> float`.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f18df03a", "metadata": {}, "outputs": [], "source": [ "def euclidean_distance(x1: float, y1: float, x2: float, y2: float) -> float:\n", " pass # اكتب هنا\n", "\n", "print(euclidean_distance(x1=0, y1=0, x2=3, y2=4))\n", "print(euclidean_distance(1, 1, -2, -2))\n" ] }, { "cell_type": "markdown", "id": "c81c5b75", "metadata": {}, "source": [ "## سحب الرصيد\n", "\n", "نمذجة سحب نقدي:\n", "\n", "1. إن كان رصيد المستخدم يكفي للمبلغ المطلوب، فتأكد أن جهاز الصراف يملك نقودًا كافية لصرفه.\n", "2. إن تحققت الشروط، اطبع رسالة نجاح مناسبة؛ وإلا اطبع سبب الرفض.\n", "\n", "**المطلوب:** الدالة `withdraw_cash(balance, amount, atm_cash)` (يمكنك إضافة تلميحات الأنواع إن رغبت).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b1a13654", "metadata": {}, "outputs": [], "source": [ "def withdraw_cash(balance, amount, atm_cash):\n", " pass # اكتب هنا\n", "\n", "withdraw_cash(500, 200, 1000)\n", "withdraw_cash(500, 200, 100)\n", "withdraw_cash(500, 600, 1000)\n" ] } ], "metadata": { "kernelspec": { "display_name": "pythonia", "language": "python", "name": "pythonia" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }