diff --git a/02_exercises/Day-4.ipynb b/02_exercises/Day-4.ipynb index ebef858..934a26d 100644 --- a/02_exercises/Day-4.ipynb +++ b/02_exercises/Day-4.ipynb @@ -1,237 +1,490 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "7c0bd9da", - "metadata": {}, - "source": [ - "# Day 4 Exercises\n", - "## Object Oriented Programming" - ] + "cells": [ + { + "cell_type": "markdown", + "id": "7c0bd9da", + "metadata": { + "id": "7c0bd9da" + }, + "source": [ + "# Day 4 Exercises\n", + "## Object Oriented Programming" + ] + }, + { + "cell_type": "markdown", + "id": "6bdfa3e9", + "metadata": { + "id": "6bdfa3e9" + }, + "source": [ + "#### 1. Create a class called `Book` that has the attributes title, author, pages, price. It also has the method get_price() which returns price, and set_price() to ensure price is numeric.\n", + "\n", + "Then create two instances of this class:\n", + "\n", + "b1:\n", + "* title -> 'Practical Programming'\n", + "* author -> 'Gries, Campbell, Montojo'\n", + "* pages -> 383\n", + "* price -> 50\n", + "\n", + "b2:\n", + "* title -> 'Building a Career in Data Science'\n", + "* author -> 'Robinson, Nolis'\n", + "* pages -> 322\n", + "* price -> 40" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "4f378a91", + "metadata": { + "id": "4f378a91" + }, + "outputs": [], + "source": [ + "# Your code goes here\n", + "class Book:\n", + " def __init__(self, title, author, pages, price):\n", + " self.title = title\n", + " self.author = author\n", + " self.pages = pages\n", + " self.price = price\n", + "\n", + " def get_price(self):\n", + " return self.price\n", + "\n", + " def set_price(self, price):\n", + " self.price = price(int)\n", + " return self.price\n" + ] + }, + { + "cell_type": "code", + "source": [ + "B1 = Book('Practical Programming', 'Gries, Campbell, Montojo', 383, 50)\n", + "B2 = Book('Building a Career in Data Science', 'Robinson, Nolis', 322, 40)" + ], + "metadata": { + "id": "QPiJPdFK1O7E" + }, + "id": "QPiJPdFK1O7E", + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "get_price = B1.get_price()\n", + "print(get_price)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wt0HUGrb1dD_", + "outputId": "0e0b15f4-1a06-453a-8b8a-e159d6db1cd1" + }, + "id": "wt0HUGrb1dD_", + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "50\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "id": "eeeea49e", + "metadata": { + "id": "eeeea49e" + }, + "source": [ + "#### 2. Create a `Phone` class that has the attributes model, id, name, brand and price. Create the getters and setters for each attribute. As well as the following methods: calculate_total which takes the price and calculates it by the tax argument." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ef1d6c93", + "metadata": { + "id": "ef1d6c93" + }, + "outputs": [], + "source": [ + "# Your code goes here\n", + "class Phone:\n", + " def __init__(self, model, id, name, brand, price):\n", + " self.model = model\n", + " self.id = id\n", + " self.name = name\n", + " self.brand = brand\n", + " self.price = price\n", + "\n", + " def get_total(self, tax):\n", + " self.tax = tax\n", + " return self.price * tax\n" + ] + }, + { + "cell_type": "markdown", + "id": "1ad55cea", + "metadata": { + "id": "1ad55cea" + }, + "source": [ + "## Numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9b1f4547", + "metadata": { + "id": "9b1f4547" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "id": "a7784ff1", + "metadata": { + "id": "a7784ff1" + }, + "source": [ + "#### 1. Write a code to convert a 1D array to a 2D array with 2 rows.\n", + "\n", + "Original Array: [0 1 2 3 4 5]\n", + "\n", + "Reshaped 2x3 Array:\n", + "\n", + "[[0 1 2]\n", + "\n", + "[3 4 5]]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "92b04fb4", + "metadata": { + "id": "92b04fb4" + }, + "outputs": [], + "source": [ + "# Your code goes here\n", + "array = np.array([0,1,2,3,4,5])\n" + ] + }, + { + "cell_type": "code", + "source": [ + "reshaped_array = array.reshape(2,3)\n", + "print(reshaped_array)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "diNE1Nfx3zK6", + "outputId": "aa327135-bbbb-4383-8f2e-7be098b63bc4" + }, + "id": "diNE1Nfx3zK6", + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0 1 2]\n", + " [3 4 5]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "id": "cf064844", + "metadata": { + "id": "cf064844" + }, + "source": [ + "#### 2. Create a 3×3 NumPy array of all True\n", + "\n", + "*Hint! Use np.ones() with dtype=bool.*" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "803e014e", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "803e014e", + "outputId": "ad15135b-8faa-489f-bcc0-bca5c22e53e5" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[ True True True]\n", + " [ True True True]\n", + " [ True True True]]\n" + ] + } + ], + "source": [ + "# Your code goes here\n", + "array = np.ones((3,3), dtype=bool)\n", + "print(array)" + ] + }, + { + "cell_type": "markdown", + "id": "f7e3f9c7", + "metadata": { + "id": "f7e3f9c7" + }, + "source": [ + "#### 3. Create a 1D array filled with zeros and another filled with ones" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e550ec75", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e550ec75", + "outputId": "5f3a5d0d-04e6-47e2-83f4-e702aa84f9dc" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Array of zeros: [0. 0. 0. 0. 0.]\n", + "Array of ones: [1. 1. 1. 1. 1. 1. 1.]\n" + ] + } + ], + "source": [ + "# Your code goes here\n", + "import numpy as np\n", + "zeros_array = np.zeros(5)\n", + "print(\"Array of zeros:\", zeros_array)\n", + "ones_array = np.ones(7)\n", + "print(\"Array of ones:\", ones_array)" + ] + }, + { + "cell_type": "markdown", + "id": "1673c3d0", + "metadata": { + "id": "1673c3d0" + }, + "source": [ + "#### 4. Reverse a 1D NumPy array\n", + "Ex. arr = np.arange(10)\n", + "\n", + "Output is [9 8 7 6 5 4 3 2 1 0]\n", + "\n", + "*Hint! Use slicing with step = -1.*" + ] + }, + { + "cell_type": "code", + "source": [ + "arr = np.arange(10)\n", + "print(\"Original array:\", arr)\n", + "reversed_arr = arr[::-1]\n", + "print(\"Reversed array:\", reversed_arr)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yfS1vI9VqJhj", + "outputId": "8f3816b5-5230-4ef6-fc05-b56dd7ed26a8" + }, + "id": "yfS1vI9VqJhj", + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Original array: [0 1 2 3 4 5 6 7 8 9]\n", + "Reversed array: [9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9877bbc5", + "metadata": { + "id": "9877bbc5" + }, + "outputs": [], + "source": [ + "# Your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "39556d59", + "metadata": { + "id": "39556d59" + }, + "source": [ + "#### 5. Stack these arrays horizontally\n", + "\n", + "\n", + "a = np.array([1, 2, 3])\n", + "\n", + "b = np.array([4, 5, 6])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "4df2d740", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4df2d740", + "outputId": "86aca580-33e9-4c68-a79f-984eaf975139" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1 2 3 4 5 6]\n" + ] + } + ], + "source": [ + "# Your code goes here\n", + "\n", + "a = np.array([1, 2, 3])\n", + "b = np.array([4, 5, 6])\n", + "stacked_array = np.hstack((a, b))\n", + "print(stacked_array)" + ] + }, + { + "cell_type": "markdown", + "id": "dea8db04", + "metadata": { + "id": "dea8db04" + }, + "source": [ + "#### 6. Perform arithmetic operations on two NumPy arrays element-wise\n", + "\n", + "a = np.array([1, 2, 3])\n", + "\n", + "b = np.array([4, 5, 6])\n", + "\n", + "* Add two NumPy arrays element by element.\n", + "* Multiply two NumPy arrays element by element." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "d4fb55a9", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "d4fb55a9", + "outputId": "5f1d8edb-6b75-4106-b4b1-f06c6e66e8b8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Element-by-element sum: [5 7 9]\n" + ] + } + ], + "source": [ + "# Your code goes here\n", + "\n", + "a = np.array([1, 2, 3])\n", + "\n", + "b = np.array([4, 5, 6])\n", + "\n", + "sum_array = a + b\n", + "print(\"Element-by-element sum:\", sum_array)" + ] + }, + { + "cell_type": "code", + "source": [ + "product_array = a * b\n", + "print(\"Element-by-element:\", product_array)" + ], + "metadata": { + "id": "8PJntV6ErFqn", + "outputId": "dfbf6cee-345e-4817-e69c-3e53f4cd03cc", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "id": "8PJntV6ErFqn", + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Element-by-element: [ 4 10 18]\n" + ] + } + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lcr-env", + "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.13" + }, + "colab": { + "provenance": [] + } }, - { - "cell_type": "markdown", - "id": "6bdfa3e9", - "metadata": {}, - "source": [ - "#### 1. Create a class called `Book` that has the attributes title, author, pages, price. It also has the method get_price() which returns price, and set_price() to ensure price is numeric.\n", - "\n", - "Then create two instances of this class:\n", - "\n", - "b1:\n", - "* title -> 'Practical Programming'\n", - "* author -> 'Gries, Campbell, Montojo'\n", - "* pages -> 383\n", - "* price -> 50\n", - "\n", - "b2:\n", - "* title -> 'Building a Career in Data Science'\n", - "* author -> 'Robinson, Nolis'\n", - "* pages -> 322\n", - "* price -> 40" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "4f378a91", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "eeeea49e", - "metadata": {}, - "source": [ - "#### 2. Create a `Phone` class that has the attributes model, id, name, brand and price. Create the getters and setters for each attribute. As well as the following methods: calculate_total which takes the price and calculates it by the tax argument." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "ef1d6c93", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "1ad55cea", - "metadata": {}, - "source": [ - "## Numpy" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9b1f4547", - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np" - ] - }, - { - "cell_type": "markdown", - "id": "a7784ff1", - "metadata": {}, - "source": [ - "#### 1. Write a code to convert a 1D array to a 2D array with 2 rows.\n", - "\n", - "Original Array: [0 1 2 3 4 5]\n", - "\n", - "Reshaped 2x3 Array:\n", - "\n", - "[[0 1 2]\n", - "\n", - "[3 4 5]]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "92b04fb4", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "cf064844", - "metadata": {}, - "source": [ - "#### 2. Create a 3×3 NumPy array of all True\n", - "\n", - "*Hint! Use np.ones() with dtype=bool.*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "803e014e", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "f7e3f9c7", - "metadata": {}, - "source": [ - "#### 3. Create a 1D array filled with zeros and another filled with ones" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e550ec75", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "1673c3d0", - "metadata": {}, - "source": [ - "#### 4. Reverse a 1D NumPy array\n", - "Ex. arr = np.arange(10)\n", - "\n", - "Output is [9 8 7 6 5 4 3 2 1 0]\n", - "\n", - "*Hint! Use slicing with step = -1.*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9877bbc5", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "39556d59", - "metadata": {}, - "source": [ - "#### 5. Stack these arrays horizontally\n", - "\n", - "\n", - "a = np.array([1, 2, 3])\n", - "\n", - "b = np.array([4, 5, 6])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4df2d740", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "dea8db04", - "metadata": {}, - "source": [ - "#### 6. Perform arithmetic operations on two NumPy arrays element-wise\n", - "\n", - "a = np.array([1, 2, 3])\n", - "\n", - "b = np.array([4, 5, 6])\n", - "\n", - "* Add two NumPy arrays element by element.\n", - "* Multiply two NumPy arrays element by element." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d4fb55a9", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "lcr-env", - "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.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file