diff --git a/day_4_exercise.ipynb b/day_4_exercise.ipynb new file mode 100644 index 0000000..acf6a47 --- /dev/null +++ b/day_4_exercise.ipynb @@ -0,0 +1,548 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyOF2EwgjKRNHrysOuV11t63", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Day 4 Exercises" + ], + "metadata": { + "id": "dsaIpSW2z38S" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Object Oriented Programming" + ], + "metadata": { + "id": "_ROpzwV5z6Pi" + } + }, + { + "cell_type": "markdown", + "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." + ], + "metadata": { + "id": "Ub-SYdLlz9DL" + } + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "_EuRBpqfz0z3" + }, + "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", + "\n", + " def get_price(self):\n", + " return self.price\n", + "\n", + " def set_price(self, price):\n", + " self.price = price" + ] + }, + { + "cell_type": "markdown", + "source": [ + "Then create two instances of this class:\n", + "\n", + "b1:\n", + "\n", + "* title -> 'Practical Programming'\n", + "* author -> 'Gries, Campbell, Montojo'\n", + "* pages -> 383\n", + "* price -> 50\n", + "\n", + "b2:\n", + "\n", + "* title -> 'Building a Career in Data Science'\n", + "* author -> 'Robinson, Nolis'\n", + "* pages -> 322\n", + "* price -> 40" + ], + "metadata": { + "id": "OluGZfk_0ESN" + } + }, + { + "cell_type": "code", + "source": [ + "b1 = Book(\"Practical Programming\", \"Gries, Campbell, Montojo\", \"383\", \"50\")" + ], + "metadata": { + "id": "FwGjxhUo1t59" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "b2 = Book(\"Building a Career in Data Science\", \"Robinson, Nolis\", \"322\", \"40\")" + ], + "metadata": { + "id": "InErCFfy2Cyj" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "markdown", + "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." + ], + "metadata": { + "id": "aZPSPqEQ0QRi" + } + }, + { + "cell_type": "code", + "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", + "\n", + " def set_model(self, model):\n", + " self.model = model\n", + "\n", + " def get_id(self):\n", + " return self.id\n", + "\n", + " def set_id(self, id):\n", + " self.id = id\n", + "\n", + " def get_name(self):\n", + " return self.name\n", + "\n", + " def set_name(self, name):\n", + " self.name = name\n", + "\n", + " def get_brand(self):\n", + " return self.brand\n", + "\n", + " def set_brand(self, brand):\n", + " self.brand = brand\n", + "\n", + " def get_price(self):\n", + " return self.price\n", + "\n", + " def set_price(self, price):\n", + " self.price = price\n", + "\n", + " def calculate_total(self, tax):\n", + " return self.price * (1 + tax)" + ], + "metadata": { + "id": "a8urcV3625Oi" + }, + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Numpy" + ], + "metadata": { + "id": "s4AWbElD0U_D" + } + }, + { + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "metadata": { + "id": "YZr5pTqW0Y08" + }, + "execution_count": 21, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "1. Write a code to convert a 1D array to a 2D array with 2 rows." + ], + "metadata": { + "id": "p2WvIohJ0XCK" + } + }, + { + "cell_type": "markdown", + "source": [ + "Original Array: [0 1 2 3 4 5]\n", + "\n", + "Reshaped 2x3 Array:\n", + "\n", + "[[0 1 2]\n", + "\n", + "[3 4 5]]" + ], + "metadata": { + "id": "S0LL_IuC1A8H" + } + }, + { + "cell_type": "code", + "source": [ + "original_array = np.array([0, 1, 2, 3, 4, 5])" + ], + "metadata": { + "id": "J4jajZJx323e" + }, + "execution_count": 25, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "reshaped_array = original_array.reshape(2, 3)\n", + "print(reshaped_array)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "fqNLenhl37sL", + "outputId": "f00e2ada-351f-4afb-dbb0-98c3af99376b" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0 1 2]\n", + " [3 4 5]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "2. Create a 3×3 NumPy array of all True" + ], + "metadata": { + "id": "jNcgfBaV1FbW" + } + }, + { + "cell_type": "markdown", + "source": [ + "*Hint! Use np.ones() with dtype=bool.*" + ], + "metadata": { + "id": "0T0ZVCtA1PGj" + } + }, + { + "cell_type": "code", + "source": [ + "true_array = np.ones((3, 3), dtype=bool)\n", + "print(true_array)" + ], + "metadata": { + "id": "s8reiy3R1R_b", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d1236404-9228-4e29-94dd-6f3ebda5e4d3" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[ True True True]\n", + " [ True True True]\n", + " [ True True True]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "3. Create a 1D array filled with zeros and another filled with ones" + ], + "metadata": { + "id": "cV4rB5d11SPu" + } + }, + { + "cell_type": "code", + "source": [ + "np.zeros(5)" + ], + "metadata": { + "id": "n3loKbFV1Tdm", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8583182b-932e-4027-acc3-b0779a6b55fd" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0., 0., 0., 0., 0.])" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "source": [ + "np.ones(5)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4PVfyfTF4lnd", + "outputId": "32236d83-0903-411d-9ed1-d7f6c81afa03" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1., 1., 1., 1., 1.])" + ] + }, + "metadata": {}, + "execution_count": 38 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "4. Reverse a 1D NumPy array" + ], + "metadata": { + "id": "VAfgJfHc1TvY" + } + }, + { + "cell_type": "markdown", + "source": [ + "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.*" + ], + "metadata": { + "id": "x2tWHwco1ZvU" + } + }, + { + "cell_type": "code", + "source": [ + "arr = np.arange(10)\n", + "arr[::-1]" + ], + "metadata": { + "id": "eUvsqdIV1dnY", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "32a898c4-9e0f-4f3e-d06f-efcaabcfdcbc" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])" + ] + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "5. Stack these arrays horizontally" + ], + "metadata": { + "id": "f42E82-I1d55" + } + }, + { + "cell_type": "markdown", + "source": [ + "a = np.array([1, 2, 3])\n", + "\n", + "b = np.array([4, 5, 6])" + ], + "metadata": { + "id": "tjXZhSgW1gww" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3])\n", + "b = np.array([4, 5, 6])\n", + "np.hstack((a, b))" + ], + "metadata": { + "id": "uax4gR021hj-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9f0fd136-07b5-4622-d6fb-7a43d4e31ea1" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6])" + ] + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "6. Perform arithmetic operations on two NumPy arrays element-wise" + ], + "metadata": { + "id": "pXTDfmv11jtS" + } + }, + { + "cell_type": "markdown", + "source": [ + "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." + ], + "metadata": { + "id": "7rURDxln1sVO" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.array([1,2,3])\n", + "b = np.array([4,5,6])" + ], + "metadata": { + "id": "0DpNBrR21xN_" + }, + "execution_count": 45, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "sum = a + b\n", + "print(sum)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KjjKbJzu6KVI", + "outputId": "f792a8b5-e710-41b6-f141-161e8cbc5f1a" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[5 7 9]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "product = a * b\n", + "print(product)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "fvZ6olnt56k9", + "outputId": "df66ab74-eca4-4c39-faba-ff8f484fc0f8" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[ 4 10 18]\n" + ] + } + ] + } + ] +} \ No newline at end of file