diff --git a/python_exercises_easy.ipynb b/2202058.ipynb similarity index 100% rename from python_exercises_easy.ipynb rename to 2202058.ipynb diff --git a/2202058_medium.ipynb b/2202058_medium.ipynb new file mode 100644 index 0000000..709810f --- /dev/null +++ b/2202058_medium.ipynb @@ -0,0 +1,561 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "58f3c807", + "metadata": {}, + "source": [ + "# Python Exercises - Medium Level\n", + "\n", + "These exercises test intermediate Python concepts including:\n", + "- Functions with multiple parameters and return values\n", + "- List comprehensions\n", + "- Dictionary operations\n", + "- Advanced string manipulation\n", + "- Nested loops and conditionals\n", + "- Working with multiple data structures\n", + "\n", + "**Instructions:** Complete each exercise by writing Python code in the provided cells." + ] + }, + { + "cell_type": "markdown", + "id": "2eaf75cb", + "metadata": {}, + "source": [ + "## Question 1: Function with Multiple Return Values\n", + "\n", + "Write a function called `analyze_numbers` that:\n", + "- Takes a list of numbers as input\n", + "- Returns a tuple containing: (sum, average, maximum, minimum)\n", + "- Test with the list: [4, 7, 2, 9, 1, 6, 8, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "beb2582b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(40, 5.0, 9, 1)\n" + ] + } + ], + "source": [ + "\n", + "\n", + "def analyze_numbers(numbers):\n", + " \n", + " total = sum(numbers)\n", + " \n", + " avg = total /len(numbers)\n", + " maximum = max(numbers)\n", + " minimun = min(numbers)\n", + " return total, avg, maximum, minimun\n", + "\n", + "test_list = [4, 7, 2, 9, 1, 6, 8, 3]\n", + "result = analyze_numbers(test_list)\n", + "print(result)\n" + ] + }, + { + "cell_type": "markdown", + "id": "32aa97ad", + "metadata": {}, + "source": [ + "## Question 2: List Comprehension Challenge\n", + "\n", + "Given the list `numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, create the following using list comprehensions:\n", + "- A list of squares of all even numbers\n", + "- A list of numbers divisible by 3\n", + "- A list of strings saying \"[number] is odd\" for odd numbers only" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "8745c171", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 6, 9] is odd\n" + ] + } + ], + "source": [ + "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "\n", + "n = [m for m in range(1, len(numbers)) if m % 3 == 0]\n", + "print(n , \"is odd\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "e21948e9", + "metadata": {}, + "source": [ + "## Question 3: Dictionary Operations\n", + "\n", + "Create a dictionary called `student_grades` with the following data:\n", + "- \"Alice\": [85, 90, 78, 92]\n", + "- \"Bob\": [76, 88, 81, 79]\n", + "- \"Charlie\": [92, 95, 87, 91]\n", + "\n", + "Write a function that:\n", + "- Calculates the average grade for each student\n", + "- Returns a new dictionary with student names as keys and average grades as values\n", + "- Finds and prints the student with the highest average" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b1f8c090", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average Grades : {'Alice': 86.25, 'Bob': 81.0, 'Charlie': 91.25}\n", + "Highest Average : 91.25\n", + "Top Student : ['Charlie']\n" + ] + } + ], + "source": [ + "student_grades = {\n", + " \"Alice\" : [85, 90, 78, 92],\n", + " \"Bob\" : [76, 88, 81, 79],\n", + " \"Charlie\" : [92, 95, 87, 91]\n", + "}\n", + "\n", + "def calculate_avg_grades(grades_dict):\n", + " average = {}\n", + " for student, grades in grades_dict.items():\n", + " average[student] = sum(grades) / len(grades)\n", + " return average\n", + "\n", + "avg_grades = calculate_avg_grades(student_grades)\n", + "\n", + "highest_avg = max(avg_grades.values())\n", + "top_students = [student for student, avg in avg_grades.items() if avg == highest_avg]\n", + "\n", + "print(\"Average Grades : \", avg_grades)\n", + "print(\"Highest Average : \", highest_avg)\n", + "print(\"Top Student : \", top_students)" + ] + }, + { + "cell_type": "markdown", + "id": "34bc3073", + "metadata": {}, + "source": [ + "## Question 4: String Processing\n", + "\n", + "Write a function called `word_frequency` that:\n", + "- Takes a sentence as input\n", + "- Returns a dictionary with each word as a key and its frequency as the value\n", + "- Words should be case-insensitive\n", + "- Test with: \"The quick brown fox jumps over the lazy dog the dog was lazy\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "245662f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'the': 3, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 2, 'dog': 2, 'was': 1}\n" + ] + } + ], + "source": [ + "\n", + "def word_frequency(sentence):\n", + " \n", + " sentence = sentence.lower()\n", + " words = sentence.split()\n", + "\n", + " frequency = {}\n", + "\n", + " for word in words:\n", + " if word in frequency :\n", + " frequency[word] += 1\n", + " else:\n", + " frequency[word] = 1\n", + "\n", + " return frequency\n", + "\n", + "\n", + "Test_sentence = \"The quick brown fox jumps over the lazy dog the dog was lazy\"\n", + "print(word_frequency(Test_sentence))" + ] + }, + { + "cell_type": "markdown", + "id": "fc1dcd37", + "metadata": {}, + "source": [ + "## Question 5: Nested Loops and Pattern\n", + "\n", + "Write a function called `multiplication_table` that:\n", + "- Takes a number `n` as input\n", + "- Prints a multiplication table from 1 to n\n", + "- Format the output nicely with proper alignment\n", + "- Test with n = 5" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "9305e600", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 x 1 = 5\n", + "5 x 2 = 10\n", + "5 x 3 = 15\n", + "5 x 4 = 20\n", + "5 x 5 = 25\n" + ] + } + ], + "source": [ + "def multiplication_table():\n", + " n = int(input(\"Enter a number : \"))\n", + " for i in range(1, n+1):\n", + " print(f\"{n} x {i} = {n*i}\") \n", + " \n", + "multiplication_table()" + ] + }, + { + "cell_type": "markdown", + "id": "b9ba0917", + "metadata": {}, + "source": [ + "## Question 6: List Manipulation\n", + "\n", + "Write a function called `remove_duplicates` that:\n", + "- Takes a list as input\n", + "- Returns a new list with duplicates removed while preserving the original order\n", + "- Don't use the set() function\n", + "- Test with: [1, 2, 2, 3, 4, 4, 5, 1, 6]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "725abfbc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7]\n" + ] + } + ], + "source": [ + "def remove_duplicates():\n", + " li = list(map(int,input(\"Enter a list : \").split()))\n", + "\n", + " new_list = []\n", + " for i in li:\n", + " if i not in new_list:\n", + " new_list.append(i)\n", + "\n", + " return new_list\n", + "print(remove_duplicates())\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "fabb21b6", + "metadata": {}, + "source": [ + " Question 7: Advanced String Formatting\n", + "\n", + "Given the following data about products:\n", + "```python\n", + "products = [\n", + " {\"name\": \"Laptop\", \"price\": 999.99, \"quantity\": 5},\n", + " {\"name\": \"Mouse\", \"price\": 25.50, \"quantity\": 20},\n", + " {\"name\": \"Keyboard\", \"price\": 75.00, \"quantity\": 15}\n", + "]\n", + "```\n", + "\n", + "Create a formatted report that shows:\n", + "- Product name\n", + "- Price\n", + "- Quantity\n", + "- Total value (price × quantity, right-aligned, 2 decimal places)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "cb37822a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product name Price Quantity Total Value\n", + "__________________________________________________\n", + "Laptop 999.99 5 4999.95 \n", + "Mouse 25.50 20 510.00 \n", + "Keyboard 75.00 15 1125.00 \n" + ] + } + ], + "source": [ + "products = [\n", + " {\"name\": \"Laptop\", \"price\": 999.99, \"quantity\": 5},\n", + " {\"name\": \"Mouse\", \"price\": 25.50, \"quantity\": 20},\n", + " {\"name\": \"Keyboard\", \"price\": 75.00, \"quantity\": 15}\n", + "]\n", + "\n", + "print(f\"{'Product name':<15}{'Price':<10}{'Quantity':<10}{'Total Value':>15}\")\n", + "print(\"_\" * 50)\n", + "\n", + "for p in products:\n", + " total_value = p['price']*p['quantity']\n", + " print(f\"{p['name']:<15}{p['price']:<10.2f}{p['quantity']:<10}{total_value:<15.2f}\")" + ] + }, + { + "cell_type": "markdown", + "id": "f2458f23", + "metadata": {}, + "source": [ + "## Question 8: Function as Parameter\n", + "\n", + "Write a function called `apply_operation` that:\n", + "- Takes a list of numbers and a function as parameters\n", + "- Applies the function to each number in the list\n", + "- Returns a new list with the results\n", + "\n", + "Then create two simple functions:\n", + "- `square(x)`: returns x squared\n", + "- `cube(x)`: returns x cubed\n", + "\n", + "Test `apply_operation` with both functions using the list [1, 2, 3, 4, 5]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e6694ca2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Squared : [1, 4, 9, 16]\n", + "Cubed : [1, 8, 27, 64]\n" + ] + } + ], + "source": [ + "def apply_function(numbers, func):\n", + " result = []\n", + " for n in numbers:\n", + " result.append(func(n))\n", + " return result\n", + "\n", + "def square(x):\n", + " return x ** 2\n", + "\n", + "def cube(x):\n", + " return x ** 3\n", + "\n", + "nums = [1, 2, 3,4]\n", + "\n", + "squared = apply_function(nums, square)\n", + "print(\"Squared : \", squared)\n", + "\n", + "cubed = apply_function(nums, cube)\n", + "print(\"Cubed : \", cubed)" + ] + }, + { + "cell_type": "markdown", + "id": "501a6617", + "metadata": {}, + "source": [ + "## Question 9: Data Processing Challenge\n", + "\n", + "Given a list of dictionaries representing students:\n", + "```python\n", + "students = [\n", + " {\"name\": \"Alice\", \"age\": 20, \"major\": \"Computer Science\", \"gpa\": 3.8},\n", + " {\"name\": \"Bob\", \"age\": 19, \"major\": \"Mathematics\", \"gpa\": 3.6},\n", + " {\"name\": \"Charlie\", \"age\": 21, \"major\": \"Computer Science\", \"gpa\": 3.9},\n", + " {\"name\": \"Diana\", \"age\": 20, \"major\": \"Physics\", \"gpa\": 3.7}\n", + "]\n", + "```\n", + "\n", + "Write functions to:\n", + "1. Find all students with GPA above 3.7\n", + "2. Group students by major\n", + "3. Find the average age of students in each major" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "d30a94a9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Students with GPA > 3.7 : \n", + "[{'name': 'Alice', 'age': 20, 'major': 'Computer Science', 'gpa': 3.8}, {'name': 'Charlie', 'age': 21, 'major': 'Computer Science', 'gpa': 3.9}]\n", + "\n", + "Grouped by major : \n", + "{'Computer Science': [{'name': 'Alice', 'age': 20, 'major': 'Computer Science', 'gpa': 3.8}]}\n", + "\n", + "Average_age_by_major : \n", + "{'Computer Science': 20.0}\n" + ] + } + ], + "source": [ + "students = [\n", + " {\"name\": \"Alice\", \"age\": 20, \"major\": \"Computer Science\", \"gpa\": 3.8},\n", + " {\"name\": \"Bob\", \"age\": 19, \"major\": \"Mathematics\", \"gpa\": 3.6},\n", + " {\"name\": \"Charlie\", \"age\": 21, \"major\": \"Computer Science\", \"gpa\": 3.9},\n", + " {\"name\": \"Diana\", \"age\": 20, \"major\": \"Physics\", \"gpa\": 3.7}\n", + "]\n", + "\n", + "def student_with_high_gpa(students,threshold=3.7):\n", + " return [s for s in students if s[\"gpa\"] > threshold]\n", + "def group_by_major(students):\n", + " grouped = {}\n", + " for s in students:\n", + " grouped.setdefault(s[\"major\"], []).append(s)\n", + " return grouped\n", + "def average_age_by_major(students):\n", + " grouped = group_by_major(students)\n", + " return {major: sum(s[\"age\"] for s in group) / len(group) for major, group in grouped.items()}\n", + "print(\"Students with GPA > 3.7 : \")\n", + "print(student_with_high_gpa(students))\n", + "\n", + "print(\"\\nGrouped by major : \")\n", + "print(group_by_major(students))\n", + "\n", + "print(\"\\nAverage_age_by_major : \")\n", + "print(average_age_by_major(students))" + ] + }, + { + "cell_type": "markdown", + "id": "58f3fcde", + "metadata": {}, + "source": [ + "## Question 10: Password Validator\n", + "\n", + "Write a function called `validate_password` that checks if a password meets these criteria:\n", + "- At least 8 characters long\n", + "- Contains at least one uppercase letter\n", + "- Contains at least one lowercase letter\n", + "- Contains at least one digit\n", + "- Contains at least one special character (!@#$%^&*)\n", + "\n", + "The function should return a dictionary with:\n", + "- \"valid\": True/False\n", + "- \"messages\": List of specific requirements that failed\n", + "\n", + "Test with passwords: \"Password123!\", \"weak\", \"NoDigits!\"" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "27ef3283", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'valid': True, 'messages': []}\n", + "{'valid': False, 'messages': ['Password must be at least 8 characters long', 'Password must contain at least one upper case letter', 'Password must contain at least one digit', 'Password must contain at least one special character (!@#$%^&*)']}\n", + "{'valid': False, 'messages': ['Password must contain at least one digit']}\n" + ] + } + ], + "source": [ + "import re \n", + "\n", + "def validate_password(password):\n", + " messages = []\n", + " special_characters = \"!@#$%^&*\"\n", + "\n", + " if len(password) < 8:\n", + " messages.append(\"Password must be at least 8 characters long\")\n", + " if not any(c.isupper() for c in password):\n", + " messages.append(\"Password must contain at least one upper case letter\")\n", + " if not any(c.islower() for c in password):\n", + " messages.append(\"Password must contain at least one lower case letter\")\n", + " if not any(c.isdigit() for c in password):\n", + " messages.append(\"Password must contain at least one digit\")\n", + " if not any(c in special_characters for c in password): \n", + " messages.append(\"Password must contain at least one special character (!@#$%^&*)\")\n", + " return {\n", + " \"valid\" : len(messages) == 0, \n", + " \"messages\" : messages\n", + " }\n", + "\n", + "print(validate_password(\"Password123!\"))\n", + "print(validate_password(\"weak\"))\n", + "print(validate_password(\"NoDigit!\"))\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}