diff --git a/02_exercises/Day_4_Exercises.ipynb b/02_exercises/Day_4_Exercises.ipynb new file mode 100644 index 0000000..4953520 --- /dev/null +++ b/02_exercises/Day_4_Exercises.ipynb @@ -0,0 +1,489 @@ +{ + "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": [ + "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", + " def get_price(self):\n", + " return self.price\n", + " def set_price(self, price):\n", + " self.price = price\n", + " return self.price\n", + "\n", + "b1 = Book('Practical Programming', 'Gries, Campbell, Montojo', 383, 50)\n", + "b2 = Book('Building a Career in Data Science', 'Robinson, Nolis', 322, 40)" + ] + }, + { + "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": 2, + "id": "ef1d6c93", + "metadata": { + "id": "ef1d6c93" + }, + "outputs": [], + "source": [ + "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_model(self):\n", + " return self.model\n", + " def set_model(self, model):\n", + " self.model = model\n", + " return self.model\n", + "\n", + " def get_id(self):\n", + " return self.id\n", + " def set_id(self, id):\n", + " self.id = id\n", + " return self.id\n", + "\n", + " def get_name(self):\n", + " return self.name\n", + " def set_name(self, name):\n", + " self.name = name\n", + " return self.name\n", + "\n", + " def get_brand(self):\n", + " return self.brand\n", + " def set_brand(self, brand):\n", + " self.brand = brand\n", + " return self.brand\n", + "\n", + " def get_price(self):\n", + " return self.price\n", + " def set_price(self, price):\n", + " self.price = price\n", + " return self.price\n", + "\n", + " def calculate_total(self, tax):\n", + " return self.price + (self.price * tax)" + ] + }, + { + "cell_type": "markdown", + "id": "1ad55cea", + "metadata": { + "id": "1ad55cea" + }, + "source": [ + "## Numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9b1f4547", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9b1f4547", + "outputId": "68366b32-089f-4f90-ffec-93aa7d0e81c8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1 2 3 4 5]\n", + "\n", + "1\n", + "(5,)\n", + "int64\n", + "5\n", + "8\n", + "40\n", + "(8,)\n", + "\n", + " C_CONTIGUOUS : True\n", + " F_CONTIGUOUS : True\n", + " OWNDATA : True\n", + " WRITEABLE : True\n", + " ALIGNED : True\n", + " WRITEBACKIFCOPY : False\n", + "\n", + "\n", + "None\n", + "[1 2 3 4 5]\n", + "[1 2 3 4 5]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "arr = np.array([1, 2, 3, 4, 5])\n", + "\n", + "print(arr)\n", + "\n", + "print(type(arr))\n", + "\n", + "print(arr.ndim)\n", + "\n", + "print(arr.shape)\n", + "\n", + "print(arr.dtype)\n", + "\n", + "print(arr.size)\n", + "\n", + "print(arr.itemsize)\n", + "\n", + "print(arr.nbytes)\n", + "\n", + "print(arr.strides)\n", + "\n", + "print(arr.data)\n", + "\n", + "print(arr.flags)\n", + "\n", + "print(arr.ctypes)\n", + "\n", + "print(arr.base)\n", + "\n", + "print(arr.view())\n", + "\n", + "print(arr.copy())" + ] + }, + { + "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": 6, + "id": "92b04fb4", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "92b04fb4", + "outputId": "df20db44-8b45-4da4-91b4-1b69033a4a4f" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0 1 2]\n", + " [3 4 5]]\n", + "(2, 3)\n", + "2\n" + ] + } + ], + "source": [ + "arr = np.array([0, 1, 2, 3, 4, 5])\n", + "arr = arr.reshape(2, 3)\n", + "print(arr)\n", + "print(arr.shape)\n", + "print(arr.ndim)" + ] + }, + { + "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": 7, + "id": "803e014e", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "803e014e", + "outputId": "61830063-13f3-4649-8d48-5f42937dcc38" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[ True True True]\n", + " [ True True True]\n", + " [ True True True]]\n" + ] + } + ], + "source": [ + "true_arr = np.ones((3, 3), dtype=bool)\n", + "print(true_arr)" + ] + }, + { + "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": 8, + "id": "e550ec75", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e550ec75", + "outputId": "370a884b-a61d-4fbf-b15b-46a572bd5184" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n" + ] + } + ], + "source": [ + "zero_arr = np.zeros(10)\n", + "one_arr = np.ones(10)\n", + "print(zero_arr)\n", + "print(one_arr)" + ] + }, + { + "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", + "execution_count": 9, + "id": "9877bbc5", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9877bbc5", + "outputId": "85461368-5b29-4448-aaee-37dfbb1646de" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ], + "source": [ + "arr = np.arange(10)\n", + "print(arr)\n", + "print(arr[::-1])" + ] + }, + { + "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": 10, + "id": "4df2d740", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4df2d740", + "outputId": "2ed3df9e-d3a3-4a02-f4f9-be3e83eff12a" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1 2 3 4 5 6]\n" + ] + } + ], + "source": [ + "a = np.array([1, 2, 3])\n", + "b = np.array([4, 5, 6])\n", + "print(np.hstack((a, b)))" + ] + }, + { + "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": 11, + "id": "d4fb55a9", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "d4fb55a9", + "outputId": "42ab099f-4660-4d26-cd0d-513c6b3724be" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[5 7 9]\n", + "[ 4 10 18]\n" + ] + } + ], + "source": [ + "a = np.array([1, 2, 3])\n", + "b = np.array([4, 5, 6])\n", + "print(a + b)\n", + "print(a * b)" + ] + } + ], + "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": [] + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file