diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..dab5c12 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,393 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Lists.ipynb", + "version": "0.3.2", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "metadata": { + "id": "e0R1W0Vzm4UU", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Exercise - List" + ] + }, + { + "metadata": { + "id": "TrO7XNQnnQZ7", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "1) Create any random list and assign it to a variable dummy_list" + ] + }, + { + "metadata": { + "id": "bjl-2QkznWid", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import random\n", + "dummy_list = [0]\n", + "for x in range(10):\n", + " x = random.randint(1,999)\n", + " dummy_list.append(x)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) print dummy_list" + ] + }, + { + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "outputId": "498ef785-47a4-4a5b-ef9b-8f536a7e9928", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 354, 225, 129, 442, 101, 71, 584, 580, 553, 216]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "metadata": { + "id": "bYa9gFOOn-4o", + "colab_type": "code", + "outputId": "f4d178ac-5227-4ab0-a9a5-0ddfedbe94a5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[216, 553, 580, 584, 71, 101, 442, 129, 225, 354, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EShv0nfXpUys", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list" + ] + }, + { + "metadata": { + "id": "Ngkc7hnYphg6", + "colab_type": "code", + "outputId": "2507ad53-6f9e-4bed-e74f-e174452b7ca5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "x = len(dummy_list_2)\n", + "for i in range(0,x): #to eliminate the '[]' from the list\n", + " dummy_list.append(dummy_list_2[i])\n", + "print(dummy_list)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[216, 553, 580, 584, 71, 101, 442, 129, 225, 354, 0, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Le1aRTuYoDzS", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. " + ] + }, + { + "metadata": { + "id": "VHfSR_Csthnk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_dict = {}\n", + "total = len(dummy_list)\n", + "for i in range(total):\n", + " temp = dummy_list[i]\n", + " dummy_dict[temp] = dummy_list.count(temp)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "rtPC6WkSos-y", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) print dummy_dict" + ] + }, + { + "metadata": { + "id": "qe5E5IgxpTWU", + "colab_type": "code", + "outputId": "d3c5edcf-e7f5-4790-9a27-7654e7412a96", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{216: 1, 553: 1, 580: 1, 584: 1, 71: 1, 101: 1, 442: 1, 129: 1, 225: 1, 354: 1, 0: 2, 2: 1, 200: 1, 16: 1, 4: 1, 1: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8n_nsBDup4--", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Sort dummy_list in ascending order as well as descending order and print the changed lists " + ] + }, + { + "metadata": { + "id": "Z_m7vr26qKnK", + "colab_type": "code", + "outputId": "b93e549e-60d2-4b22-ab22-22d723f43b8b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + } + }, + "cell_type": "code", + "source": [ + "dummy_list.sort()\n", + "print(dummy_list)\n", + "dummy_list.sort(reverse=True)\n", + "print(dummy_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 45.67, 71, 90, 101, 129, 200, 216, 225, 354, 442, 553, 580, 584]\n", + "[584, 580, 553, 442, 354, 225, 216, 200, 129, 101, 90, 71, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Znm5Qo4LqPKA", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "8) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item." + ] + }, + { + "metadata": { + "id": "1-8mlngDqYvS", + "colab_type": "code", + "outputId": "eaa954d6-91c4-4556-878b-457bd0f59a97", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x)\n", + "print(dummy_list)\n", + "\n", + "#dummy_list.remove(13) \n", + "#ValueError Traceback (most recent call last)\n", + "# in ()\n", + "# 1 x = 200\n", + "#---> 2 dummy_list.remove(x)\n", + "# 3 print(dummy_list)\n", + "# 4 dummy_list.remove(13)\n", + "# 5 \n", + "#\n", + "#ValueError: list.remove(x): x not in list\n", + "# Let's play: try the same with something which is not in the list to get the ValueError" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[584, 580, 553, 442, 354, 225, 216, 129, 101, 90, 71, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "metadata": { + "id": "NakdzAYoRLwv", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "import random\n", + "x = random.randint(0,len(dummy_list))\n", + "dummy_list.remove(x)\n", + "\n", + "#to get the value errors\n", + "#x = len(dummy_list) + 1\n", + "#dummy_list.remove(x)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "metadata": { + "id": "aMyo1gmRrVHo", + "colab_type": "code", + "outputId": "e932f27d-579e-482f-e913-77df3c2ec5e9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "dummy_list.clear()\n", + "print(dummy_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb new file mode 100644 index 0000000..5fdc567 --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,338 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Exercises.ipynb", + "version": "0.3.2", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Numpy Exercises\n", + "\n", + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "metadata": { + "id": "LIP5u4zi0Nmg", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "metadata": { + "id": "4TxT66309n1o", + "colab_type": "code", + "outputId": "c5cca493-2e3d-4ccd-f185-8ca9f485ee05", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 89 + } + }, + "cell_type": "code", + "source": [ + "n = 4\n", + "arr = np.zeros(shape=(n,3),dtype=int)\n", + "for i in range(n):\n", + " arr[i][0] = 1\n", + " arr[i][1] = 2\n", + " arr[i][2] = 3\n", + " \n", + "print(arr)" + ], + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [1 2 3]\n", + " [1 2 3]\n", + " [1 2 3]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "metadata": { + "id": "ebhEUZq29r32", + "colab_type": "code", + "outputId": "0998a44d-42b9-4012-d8a6-ff678071207b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "array_odd = np.arange(1,20,2)\n", + "print(array_odd)" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17 19]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "metadata": { + "id": "gOlfuJCo-JwF", + "colab_type": "code", + "outputId": "c75a3ae3-7829-4f51-8f8c-f40238544db1", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "cell_type": "code", + "source": [ + "#expected output array([2, 4])\n", + "a = np.array([1,2,3,2,3,4,3,4,5,6])\n", + "b = np.array([7,2,10,2,7,4,9,4,9,8])\n", + "print(np.intersect1d(a,b))" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2 4]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "metadata": { + "id": "2E8b55_2Cjx5", + "colab_type": "code", + "outputId": "0daf38d8-f8d5-4792-bea4-d1f45e99131e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + } + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "print(a.shape)\n", + "a = a.reshape(2,5)\n", + "print(a.shape)" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(10,)\n", + "(2, 5)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "metadata": { + "id": "tcBCyhXPEp9C", + "colab_type": "code", + "outputId": "5f98e987-aa52-46d0-8818-fa8e0f029fbe", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + } + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "print(type(a))\n", + "new_arr = np.array(a)\n", + "print(type(new_arr))" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "JNqX8wnz9sQJ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones." + ] + }, + { + "metadata": { + "id": "4bjP3JAc9vRD", + "colab_type": "code", + "outputId": "aad80464-6a17-4dc7-b63f-0ed9fb32079d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + } + }, + "cell_type": "code", + "source": [ + "arr = np.zeros(shape = (10,10),dtype=int)\n", + "def border(arr,min,max,bor):\n", + " arr[min, :] = 1\n", + " arr[:, min] = 1\n", + " arr[max, :] = 1\n", + " arr[:, max] = 1\n", + " return arr\n", + "\n", + "arr = border(arr,0,9,1)\n", + "print(arr)\n" + ], + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1 1 1 1 1 1 1 1 1 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 1 1 1 1 1 1 1 1 1]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xaQgf8tT9v-n", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach." + ] + }, + { + "metadata": { + "id": "No7fx0Xy9zEh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 161 + }, + "outputId": "203beecb-7925-44a6-b276-4e3da26d6469" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "arr = np.zeros((8,8),dtype=int)\n", + "arr[1::2,::2] = 1\n", + "arr[::2,1::2] = 1\n", + "print(arr)" + ], + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file