diff --git a/10_arrays.c b/10_arrays.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/10_arrays.c @@ -0,0 +1 @@ + diff --git a/F1_Debugging_.ipynb b/F1_Debugging_.ipynb new file mode 100644 index 0000000..2590e59 --- /dev/null +++ b/F1_Debugging_.ipynb @@ -0,0 +1,1238 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyNlyspq6lZvWurCDwCpjMZd" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "#**Variables and Data types**" + ], + "metadata": { + "id": "7O0OXEXIRyU8" + } + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #1: Mismatched Variable Types**\n", + "**Difficulty: Easy**\n", + "\n", + "\n", + "```\n", + "# # This program calculates the total price of items\n", + "\n", + "item_price = \"20.5\" # Price of a single item (in dollars)\n", + "quantity = 3 # Number of items purchased\n", + "\n", + "total_price = item_price * quantity\n", + "\n", + "print(\"The total price is: $\", total_price)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "fx_fjuer4rhk" + } + }, + { + "cell_type": "code", + "source": [ + "item_price = \"20.5\" # Price of a single item (in dollars)\n", + "quantity = 3 # Number of items purchased\n", + "\n", + "item_price = float(item_price)\n", + "\n", + "total_price = item_price * quantity\n", + "\n", + "print(\"The total price is: $\", total_price)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IpSqF9Zs_cXv", + "outputId": "55d91080-8398-4573-ea16-a8bd2766fc05" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total price is: $ 61.5\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #2: Incorrect Variable Usage in a Formula**\n", + "**Difficulty: Hard**\n", + "\n", + "```\n", + "# # This program calculates the simple interest on a loan\n", + "\n", + "principal = \"5000\" # Principal amount (in dollars)\n", + "rate = 5 # Annual interest rate (in percent)\n", + "time = \"2\" # Time duration (in years)\n", + "\n", + "# Formula for simple interest: SI = (principal * rate * time) / 100\n", + "simple_interest = principal * rate / time / 100\n", + "\n", + "print(\"The simple interest is: $\", simple_interest)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "9Ne77pSqB6Eu" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the simple interest on a loan\n", + "\n", + "principal = \"5000\" # Principal amount (in dollars)\n", + "rate = 5 # Annual interest rate (in percent)\n", + "time = \"2\" # Time duration (in years)\n", + "\n", + "principal = int(principal)\n", + "rate = int(rate)\n", + "time = int(time)\n", + "# Formula for simple interest: SI = (principal * rate * time) / 100\n", + "simple_interest = principal * rate / time / 100\n", + "\n", + "print(\"The simple interest is: $\", simple_interest)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "danPZUwrCat4", + "outputId": "91011899-a13f-4c28-b029-bdc44e9edd89" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The simple interest is: $ 125.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #3: Incorrect Variable Assignment and Calculation**\n", + "**Difficulty: Hard**\n", + "```\n", + "# This program calculates the total cost of a shopping cart\n", + "\n", + "item1_price = 12 # Price of item 1 (in dollars)\n", + "item2_price = 8 # Price of item 2 (in dollars)\n", + "item3_price = \"7\" # Price of item 3 (in dollars, as a string)\n", + "\n", + "discount = 5 # Discount percentage\n", + "\n", + "total_cost = item1_price + item2_price + item3_price * (1 - discount / 100)\n", + "\n", + "print(\"The total cost after discount is: $\", total_cost)\n", + "```\n" + ], + "metadata": { + "id": "Z407cmE6C6sV" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the total cost of a shopping cart\n", + "\n", + "item1_price = 12 # Price of item 1 (in dollars)\n", + "item2_price = 8 # Price of item 2 (in dollars)\n", + "item3_price = int(\"7\") # Price of item 3 (in dollars, as a string)\n", + "\n", + "discount = 5 # Discount percentage\n", + "\n", + "total_cost = item1_price + item2_price + item3_price * (1 - discount / 100)\n", + "\n", + "print(\"The total cost after discount is: $\", total_cost)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ePtPqg46DMI2", + "outputId": "50b0c883-a57c-48c0-fd01-9400db55c951" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total cost after discount is: $ 26.65\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #4: String Concatenation Error**\n", + "**Difficulty: Hard**\n", + "```\n", + "# This program calculates the age of a person in months\n", + "\n", + "birth_year = \"1990\" # Birth year as a string\n", + "birth_month = 6 # Birth month as an integer\n", + "current_year = 2024\n", + "current_month = \"11\" # Current month as a string\n", + "\n", + "age_in_months = (current_year - birth_year) * 12 + (current_month - birth_month)\n", + "\n", + "print(\"The age of the person in months is: \", age_in_months)\n" + ], + "metadata": { + "id": "ha0oJlPtMbU-" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the age of a person in months\n", + "\n", + "birth_year = int(\"1990\") # Birth year as a string\n", + "birth_month = 6 # Birth month as an integer\n", + "current_year = 2024\n", + "current_month = int(\"11\") # Current month as a string\n", + "\n", + "age_in_months = (current_year - birth_year) * 12 + (current_month - birth_month)\n", + "\n", + "print(\"The age of the person in months is: \", age_in_months)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Ewhy2Ly_Mps2", + "outputId": "b1024dd7-0ea2-4302-b39a-d8f6633adea2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The age of the person in months is: 413\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #5: Variable Shadowing and Incorrect Calculation**\n", + "**Difficulty: Hard**\n", + "```\n", + "# This program calculates the total cost of a product after applying tax\n", + "\n", + "price = 100 # Original price of the product (in dollars)\n", + "tax_rate = 0.05 # Tax rate (5%)\n", + "\n", + "def calculate_total_cost(price):\n", + " tax_rate = 0.10 # Inside the function, tax rate is incorrectly reassigned\n", + " return price + (price * tax_rate)\n", + "\n", + "total_cost = calculate_total_cost(price)\n", + "\n", + "print(\"The total cost after tax is: $\", total_cost)\n", + "```" + ], + "metadata": { + "id": "ny2hwxZoM5Rr" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the total cost of a product after applying tax\n", + "\n", + "price = 100 # Original price of the product (in dollars)\n", + "tax_rate = 0.05 # Tax rate (5%)\n", + "\n", + "def calculate_total_cost(price , tax_rate):\n", + " return price + (price * tax_rate)\n", + "\n", + "total_cost = calculate_total_cost(price , tax_rate)\n", + "\n", + "print(\"The total cost after tax is: $\", total_cost)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OHddF_qXNK5j", + "outputId": "6166b209-2783-4368-f1a1-44f1ab8b91a2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total cost after tax is: $ 105.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##Problem #6: Incorrect Calculation Due to String Multiplication**\n", + "**Difficulty: Hard**\n", + "```\n", + "# This program calculates the total amount to be paid for a subscription\n", + "\n", + "subscription_fee = \"49.99\" # Monthly subscription fee (as a string)\n", + "months = 12 # Number of months for subscription\n", + "\n", + "total_payment = subscription_fee * months # Incorrect multiplication with string\n", + "\n", + "print(\"The total amount to be paid for the subscription is: $\", total_payment)\n", + "```" + ], + "metadata": { + "id": "qHty0aPfPvxR" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the total amount to be paid for a subscription\n", + "\n", + "subscription_fee = float(\"49.99\") # Monthly subscription fee (as a string)\n", + "months = 12 # Number of months for subscription\n", + "\n", + "total_payment = subscription_fee * months # Incorrect multiplication with string\n", + "\n", + "print(\"The total amount to be paid for the subscription is: $\", total_payment)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3lCP5s_HP6om", + "outputId": "bc5971e9-6db3-4f0a-ae8b-d5da7f85a1b4" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total amount to be paid for the subscription is: $ 599.88\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## **Problem #7: Unintended Result Due to Data Type Conversion**\n", + "**Difficulty: Hard**\n", + "```\n", + "# This program calculates the total cost of purchasing multiple items\n", + "\n", + "item_price = \"15.75\" # Price of a single item (in dollars, as a string)\n", + "item_count = 4 # Number of items (as an integer)\n", + "\n", + "total_cost = item_price * item_count # Incorrect multiplication due to string type\n", + "\n", + "print(\"The total cost is: $\", total_cost)\n", + "```" + ], + "metadata": { + "id": "FTSyio7rRXSU" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the total cost of purchasing multiple items\n", + "\n", + "item_price = float(\"15.75\") # Price of a single item (in dollars, as a string)\n", + "item_count = 4 # Number of items (as an integer)\n", + "\n", + "total_cost = item_price * item_count # Incorrect multiplication due to string type\n", + "\n", + "print(\"The total cost is: $\", total_cost)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hI7w7tQbRlSQ", + "outputId": "20b8a6aa-e8a6-47b3-e9ac-622bca82409f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total cost is: $ 63.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #8: Mixing Data Types in Arithmetic Operation**\n", + "**Difficulty: Hard**\n", + "\n", + "\n", + "```\n", + "# This program calculates the average score of a student\n", + "\n", + "total_marks = \"350\" # Total marks (as a string)\n", + "subjects = 5 # Number of subjects (as an integer)\n", + "\n", + "average_score = total_marks / subjects # Incorrect division due to string type\n", + "\n", + "print(\"The average score is: \", average_score)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "hIwkmPPeSD4R" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the average score of a student\n", + "\n", + "total_marks = int(\"350\") # Total marks (as a string)\n", + "subjects = 5 # Number of subjects (as an integer)\n", + "\n", + "average_score = total_marks / subjects # Incorrect division due to string type\n", + "\n", + "print(\"The average score is: \", average_score)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7IPiGREESdHn", + "outputId": "e2829390-dfbf-4e96-f5eb-6a90170fcd85" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The average score is: 70.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## **Problem #9: Calculation Error Due to String Concatenation**\n", + "**Difficulty: Intermediate**\n", + "\n", + "\n", + "```\n", + "# This program calculates the total salary after deductions\n", + "\n", + "basic_salary = \"5000\" # Basic salary (as a string)\n", + "bonus = 1200 # Bonus amount (as an integer)\n", + "deductions = \"800\" # Deductions (as a string)\n", + "\n", + "total_salary = basic_salary + bonus - deductions # Incorrect calculation due to string concatenation\n", + "\n", + "print(\"The total salary after deductions is: $\", total_salary)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "KFSeo6c9SzFj" + } + }, + { + "cell_type": "code", + "source": [ + "# This program calculates the total salary after deductions\n", + "\n", + "basic_salary = int(\"5000\") # Basic salary (as a string)\n", + "bonus = 1200 # Bonus amount (as an integer)\n", + "deductions = int(\"800\") # Deductions (as a string)\n", + "\n", + "total_salary = basic_salary + bonus - deductions # Incorrect calculation due to string concatenation\n", + "\n", + "print(\"The total salary after deductions is: $\", total_salary)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "99RmrjRQS-s5", + "outputId": "92826637-5437-46e1-cf86-b2cd1e16bf09" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total salary after deductions is: $ 5400\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#**functions in Python**" + ], + "metadata": { + "id": "qNRXklb9TEIJ" + } + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #10: Function Return Error**\n", + "**Difficulty: Easy**\n", + "```\n", + "# This program defines a function to calculate the area of a rectangle\n", + "\n", + "def calculate_area(length, width):\n", + " area = length * width\n", + " # Missing return statement\n", + "\n", + "length = 5\n", + "width = 10\n", + "\n", + "total_area = calculate_area(length, width)\n", + "\n", + "print(\"The area of the rectangle is: \", total_area)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "hJusDS24TL2k" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function to calculate the area of a rectangle\n", + "\n", + "def calculate_area(length, width):\n", + " area = length * width\n", + " return area\n", + " # Missing return statement\n", + "\n", + "length = 5\n", + "width = 10\n", + "\n", + "total_area = calculate_area(length, width)\n", + "\n", + "print(\"The area of the rectangle is: \", total_area)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Qe3jFhUGTU-9", + "outputId": "8a86ad57-d348-41b5-a394-2d17e03eac78" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the rectangle is: 50\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #11: Function Argument Misuse**\n", + "**Difficulty: Intermediate**\n", + "\n", + "\n", + "```\n", + "# This program defines a function to calculate the total cost after applying tax\n", + "\n", + "def calculate_total_cost(price, tax_rate=0.05):\n", + " total_cost = price + price * tax_rate\n", + " return total_cost\n", + "\n", + "product_price = 200\n", + "tax_rate = 0.1\n", + "\n", + "total = calculate_total_cost(product_price, tax_rate)\n", + "\n", + "print(\"The total cost is: $\", total)\n", + "\n", + "```\n", + "\n", + "\n" + ], + "metadata": { + "id": "1hKqcoqcThgP" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function to calculate the total cost after applying tax\n", + "\n", + "def calculate_total_cost(price, tax_rate=0.05):\n", + " total_cost = price + price * tax_rate\n", + " return total_cost\n", + "\n", + "product_price = 200\n", + "tax_rate = 0.1\n", + "\n", + "total = calculate_total_cost(product_price, tax_rate)\n", + "\n", + "print(\"The total cost is: $\", total)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wfARZ4sPTugt", + "outputId": "94ac620a-82ca-40e9-8315-c58a075c4a68" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total cost is: $ 220.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #12: Function Without Arguments**\n", + "**Difficulty: Easy**\n", + "\n", + "\n", + "```\n", + "# This program defines a function that prints a greeting message\n", + "\n", + "def greet():\n", + " greeting = \"Hello, welcome to Python!\"\n", + " print(greeting)\n", + "\n", + "greeting_message = greet() # Function called but it's not returning any value\n", + "\n", + "print(\"The greeting message is: \", greeting_message)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "gVy0gN81UgWr" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function that prints a greeting message\n", + "\n", + "def greet():\n", + " greeting = \"Hello, welcome to Python!\"\n", + " return (greeting)\n", + "\n", + "greeting_message = greet() # Function called but it's not returning any value\n", + "\n", + "print(\"The greeting message is: \", greeting_message)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rz0xvFqhUngD", + "outputId": "81ed2805-0b98-4293-8be8-dd3d94aaf377" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The greeting message is: Hello, welcome to Python!\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##Problem #13: Incorrect Function Calculation**\n", + "**Difficulty: Intermediate**\n", + "\n", + "\n", + "```\n", + "# This program defines a function that calculates the area of a circle\n", + "\n", + "def calculate_area(radius):\n", + " area = 3.14 * radius * radius # Using an approximate value for pi\n", + " return area\n", + "\n", + "radius = \"5\" # Radius provided as a string\n", + "\n", + "circle_area = calculate_area(radius) # Calling the function with string argument\n", + "\n", + "print(\"The area of the circle is: \", circle_area)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "eedr5NiPVU1z" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function that calculates the area of a circle\n", + "\n", + "def calculate_area(radius):\n", + " area = 3.14 * radius * radius # Using an approximate value for pi\n", + " return area\n", + "\n", + "radius = int(\"5\") # Radius provided as a string\n", + "\n", + "circle_area = calculate_area(radius) # Calling the function with string argument\n", + "\n", + "print(\"The area of the circle is: \", circle_area)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Y2ecXM3CV3OE", + "outputId": "d7453391-484a-4b99-c481-b5ae4437ccd2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the circle is: 78.5\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #14: Missing Default Argument**\n", + "**Difficulty: Intermediate**\n", + "```# This program defines a function to calculate the total price of an order with optional discount\n", + "\n", + "def calculate_price(price, discount):\n", + " total_price = price - (price * discount)\n", + " return total_price\n", + "\n", + "item_price = 100\n", + "discount_rate = 0.1 # 10% discount\n", + "\n", + "final_price = calculate_price(item_price) # Discount argument is missing\n", + "\n", + "print(\"The final price is: $\", final_price)\n", + "```" + ], + "metadata": { + "id": "VRtAR-GCWCqA" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function to calculate the total price of an order with optional discount\n", + "\n", + "def calculate_price(price, discount):\n", + " total_price = price - (price * discount)\n", + " return total_price\n", + "\n", + "item_price = 100\n", + "discount_rate = 0.1 # 10% discount\n", + "\n", + "final_price = calculate_price(item_price , discount) # Discount argument is missing\n", + "\n", + "print(\"The final price is: $\", final_price)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KdmFsnHwWKy_", + "outputId": "bbc9ceca-0e7e-4526-953b-85b7f44b9449" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The final price is: $ -400\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #15: Function Scope Issue**\n", + "**Difficulty: Hard**\n", + "\n", + "\n", + "```\n", + "# This program defines a function to modify the global variable\n", + "\n", + "counter = 10 # Global counter variable\n", + "\n", + "def increment():\n", + " counter = counter + 1 # Trying to modify the global variable\n", + "\n", + "increment()\n", + "\n", + "print(\"Counter value is: \", counter)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "tSoO9pZlWoCb" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function to modify the global variable\n", + "\n", + "counter = 10 # Global counter variable\n", + "\n", + "def increment(counter):\n", + " counter = counter + 1 # Trying to modify the global variable\n", + "\n", + "increment(counter)\n", + "\n", + "print(\"Counter value is: \", counter)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9SDNRYMWWvMr", + "outputId": "b1e064a8-88a0-466d-ef62-a3b15ff5aaa8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Counter value is: 10\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #16: Incorrect Use of Mutable Default Argument**\n", + "**Difficulty: Hard**\\\n", + "\n", + "\n", + "```\n", + "# This program defines a function that appends items to a list\n", + "\n", + "def add_to_list(item, my_list=[]):\n", + " my_list.append(item)\n", + " return my_list\n", + "\n", + "result = add_to_list(\"apple\")\n", + "result2 = add_to_list(\"banana\")\n", + "\n", + "print(\"First list:\", result)\n", + "print(\"Second list:\", result2)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "ZOGjfSi5XF7j" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function that appends items to a list\n", + "\n", + "def add_to_list(item):\n", + " my_list.append(item)\n", + " return my_list\n", + "\n", + "my_list=[]\n", + "\n", + "result = add_to_list(\"apple\")\n", + "result2 = add_to_list(\"banana\")\n", + "\n", + "print(\"First list:\", result)\n", + "print(\"Second list:\", result2)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "UuWJKpVXXOYd", + "outputId": "7aea79e3-346d-4258-834b-bc56b5c733d0" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "First list: ['apple', 'banana']\n", + "Second list: ['apple', 'banana']\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #17: Recursion Limit Issue**\n", + "**Difficulty: Hard**\n", + "\n", + "\n", + "```\n", + "# This program defines a recursive function to calculate factorial\n", + "\n", + "def factorial(n):\n", + " if n == 0:\n", + " return 1\n", + " else:\n", + " return n * factorial(n) # Recursive call with the same value of n\n", + "\n", + "number = 5\n", + "result = factorial(number)\n", + "\n", + "print(\"The factorial of\", number, \"is:\", result)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "zOAUJL9-XtA4" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a recursive function to calculate factorial\n", + "\n", + "def factorial(n):\n", + " if n == 0:\n", + " return 1\n", + " else:\n", + " return n * factorial(n - 1) # Recursive call with the same value of n\n", + "\n", + "number = 5\n", + "result = factorial(number)\n", + "\n", + "print(\"The factorial of\", number, \"is:\", result)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "h9PYUbxrX2kX", + "outputId": "61b65e8f-f625-416b-885e-98bada36ebc8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The factorial of 5 is: 120\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #18: Function with Multiple Return Values**\n", + "**Difficulty: Intermediate**\n", + "\n", + "\n", + "```\n", + "# This program defines a function that returns multiple values\n", + "\n", + "def calculate_operations(a, b):\n", + " addition = a + b\n", + " subtraction = a - b\n", + " multiplication = a * b\n", + " # Forgot to return all values\n", + "\n", + "result = calculate_operations(10, 5)\n", + "\n", + "print(\"The results are: \", result)\n", + "```\n", + "\n" + ], + "metadata": { + "id": "uE9lnNc4ZE3q" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function that returns multiple values\n", + "\n", + "def calculate_operations(a, b):\n", + " addition = a + b\n", + " subtraction = a - b\n", + " multiplication = a * b\n", + " # Forgot to return all values\n", + " return(addition , subtraction , multiplication)\n", + "\n", + "result = calculate_operations(10, 5)\n", + "\n", + "print(\"The results are:\" , result)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "2W6zwvXcZLr7", + "outputId": "bd4de616-b381-497b-bef0-9e5f7d826cb8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The results are: (15, 5, 50)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #19: Lambda Function Issue**\n", + "**Difficulty: Intermediate**\n", + "\n", + "\n", + "```\n", + "# This program defines a lambda function to add two numbers\n", + "\n", + "add_numbers = lambda x, y: x + y\n", + "\n", + "result = add_numbers(5) # Missing second argument\n", + "\n", + "print(\"The sum is: \", result)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "ipfYpVyaaWIy" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a lambda function to add two numbers\n", + "\n", + "add_numbers = lambda x, y: x + y\n", + "\n", + "result = add_numbers(5 , y=2) # Missing second argument\n", + "\n", + "print(\"The sum is: \", result)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NrIfI5h9abWL", + "outputId": "882ddb14-14be-44af-947e-e7c35db8af64" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The sum is: 7\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "##**Problem #20: Recursive Function Limit**\n", + "**Difficulty: Hard**\n", + "\n", + "\n", + "```\n", + "# This program defines a function that uses recursion to calculate the sum of integers from 1 to n\n", + "\n", + "def sum_recursive(n):\n", + " if n == 0:\n", + " return 0\n", + " else:\n", + " return n + sum_recursive(n) # Recursive call with the same value of n\n", + "\n", + "result = sum_recursive(5)\n", + "\n", + "print(\"The sum is:\", result)\n", + "\n", + "```\n", + "\n" + ], + "metadata": { + "id": "z3dFHFwLai_i" + } + }, + { + "cell_type": "code", + "source": [ + "# This program defines a function that uses recursion to calculate the sum of integers from 1 to n\n", + "\n", + "def sum_recursive(n):\n", + " if n == 0:\n", + " return 0\n", + " else:\n", + " return n + sum_recursive(n - 1) # Recursive call with the same value of n\n", + "\n", + "result = sum_recursive(5)\n", + "\n", + "print(\"The sum is:\", result)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Efxmn7F2avHm", + "outputId": "feebc8af-35d6-43cf-d696-cf0385142169" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The sum is: 15\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# **loops (for and while loops)**" + ], + "metadata": { + "id": "V0FCcXtxbL4t" + } + }, + { + "cell_type": "markdown", + "source": [ + "## **Problem #21: Infinite Loop Issue**\n", + "**Difficulty: Easy**\n", + "\n", + "\n", + "```\n", + "# This program uses a while loop to print numbers from 1 to 5\n", + "\n", + "counter = 1\n", + "\n", + "while counter <= 5:\n", + " print(counter)\n", + " # Missing update to counter, causing an infinite loop\n", + "```\n", + "\n" + ], + "metadata": { + "id": "WE2qGXfIbPqc" + } + }, + { + "cell_type": "code", + "source": [ + "# This program uses a while loop to print numbers from 1 to 5\n", + "\n", + "counter = 1\n", + "\n", + "while counter <= 5:\n", + " print(counter)\n", + " # Missing update to counter, causing an infinite loop\n", + " counter += 1\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bZGG3PmrbZCH", + "outputId": "3af905e2-dcdb-416a-a056-ff2ee175f49c" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ] + } + ] +}