{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "title: \"مسائل الفهرسة والاستخراج\"\n", "toc: true\n", "format:\n", " html: default\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[تحميل الدفتر](https://raw.githubusercontent.com/HassanAlgoz/python/main/book/chapters/ch5-dict/exercise_5.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: استخراج عنصر بفهرس موجب\n", "\n", "لديك القائمة التالية. استخدم **فهرسة موجبة** فقط (`[0]`، `[1]`، …) لاستخراج العنصر المطلوب." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "scores = [12, 19, 7, 22, 15]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** اكتب تعبير فهرسة واحدًا يعيد `22`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: استخراج عنصر بفهرس سالب\n", "\n", "لديك القائمة نفسها. استخدم **فهرسة سالبة** فقط لاستخراج:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "scores = [12, 19, 7, 22, 15]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبيران: أحدهما يعيد `15` والآخر يعيد `22`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: استخراج تسلسل باستخدام الشرائح `start:stop`\n", "\n", "استخدم **الشريحة** (بدون خطوة) لاستخراج جزء متصل من القائمة:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "letters = ['a', 'b', 'c', 'd', 'e', 'f']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** شريحة واحدة تعيد `['c', 'd', 'e']`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: شريحة بحدود سالبة لاستخراج جزء محدّد\n", "\n", "استخدم **شريحة** يكون فيها **`start` أو `stop` (أو كلاهما) سالبًا** لاستخراج آخر عنصرين:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "letters = ['a', 'b', 'c', 'd', 'e', 'f']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبير شريحة واحد يعيد `['e', 'f']` دون استخدام أرقام موجبة داخل `[` `]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: استبدال قيمة عند فهرس معيّن\n", "\n", "غيّر القائمة **في مكانها** بحيث تصبح القيمة عند الفهرس `2` هي `99`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "values = [10, 20, 30, 40]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** سطر إسناد واحد يستخدم الفهرسة." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: إيجاد الفهرس بالقيمة ثم الاستبدال\n", "\n", "ابحث عن **أول** موضع للقيمة `20` ثم استبدلها بـ `200`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "values = [10, 20, 30, 20, 40]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** استخدم دالة تبحث بالقيمة (مثل `.index(...)`) ثم الإسناد بالفهرس." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة: كل عنصر ثان باستخدام `start:stop:step`\n", "\n", "من القائمة التالية، استخرج **كل عنصر ثان** بدءًا من الأول، ثم **كل عنصر ثان** بدءًا من الثاني:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "nums = [0, 1, 2, 3, 4, 5, 6, 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** شريحتان بصيغة `[start:stop:step]` (يمكن إهمال `start`/`stop` عند الحاجة)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة متداخلة: استخراج عنصر داخلي" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "grid = [\n", " [1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9],\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبير فهرسة متداخلة واحد يعيد العدد `6`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قاموس: استخراج قيمة بمفتاح" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "person = {'name': 'Sara', 'age': 31, 'city': 'Riyadh'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبير يعيد `'Riyadh'` (بأقواس مربعة أو `.get`)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قاموس متداخل: استخراج قيمة من مستوى داخلي" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "config = {\n", " 'app': 'demo',\n", " 'db': {'host': 'localhost', 'port': 5432},\n", " 'debug': True,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبير يعيد `5432`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة من قواميس وقوائم داخلية: أعمق قيمة في البنية\n", "\n", "البنية التالية: قائمة من قواميس؛ كل قاموس يحتوي مفتاحًا قيمته **قائمة**. استخرج قيمة من **أعمق قائمة** كما يُطلب." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| eval: false\n", "teams = [\n", " {\n", " 'name': 'Alpha',\n", " 'scores': [10, 12, 9],\n", " },\n", " {\n", " 'name': 'Beta',\n", " 'scores': [14, 11, 16, 8],\n", " },\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**المطلوب:** تعبير فهرسة/مفاتيح واحد متسلسل (قائمة → قاموس → مفتاح `scores` → فهرس داخل القائمة) يعيد `8`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قاموس داخل قاموس\n", "\n", "إذا كان لديك قاموس متداخل كالتالي:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "user_preferences = {\n", " 'theme': 'dark',\n", " 'language': 'Arabic',\n", " 'notifications': {\n", " 'email': True,\n", " 'sms': False,\n", " 'push': True\n", " },\n", " 'last_updated': '2021-09-01',\n", " 'emails': ['example1@domain.com', 'example2@domain.com']\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "المطلوب أن تسحب من تفضيلات المستخدم المتغيرات التالية:\n", "\n", "- `theme`\n", "- `sms`\n", "- آخر عنصر في: `emails`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## قائمة قواميس\n", "\n", "إليك هذه القائمة من القواميس:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = [\n", " {\n", " 'name': 'Ahmad Hamada',\n", " 'experiences': [\n", " {\n", " 'company': 'Geo Space',\n", " 'start': '2038-01-01',\n", " 'end': '2039-01-01',\n", " 'role': 'Junior Software Engineer',\n", " },\n", " {\n", " 'company': 'Space Roots',\n", " 'start': '2039-01-01',\n", " 'end': '2041-01-01',\n", " 'role': 'Senior Software Engineer',\n", " },\n", " ],\n", " },\n", " {\n", " 'name': 'Belal Banana',\n", " 'experiences': [\n", " {\n", " 'company': 'Banana Tech',\n", " 'start': '2041-01-01',\n", " 'end': '2042-01-01',\n", " 'role': 'Smoothie Operator',\n", " },\n", " {\n", " 'company': 'BugSquash Labs',\n", " 'start': '2042-02-01',\n", " 'end': '2043-08-01',\n", " 'role': 'Code Pest Control Specialist',\n", " },\n", " {\n", " 'company': 'Caffeinated Circuits Inc.',\n", " 'start': '2043-09-01',\n", " 'end': '2045-05-01',\n", " 'role': 'Espresso-Driven Engineer',\n", " }\n", " ]\n", " },\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "والمطلوب هو التالي:\n", "\n", "- استخرج اسم المرشح الثاني\n", "- استخرج اسم الشركة الثانية التي عمل فيها المشرح الأول\n", "- للمرشح الأول، استخرج وقت البداية لأول شركة، ووقت النهاية لآخر شركة\n", "- استخرج قاموس الخبرة التي فيها القيمة: `role: \"Espresso-Driven Engineer\"`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CODE HERE" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }