diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..142cb14 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,458 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Lists.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MonojitBag/Assignment-2/blob/MonojitBag/Lists.ipynb)" + ] + }, + { + "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": [ + "dummy_list=[1,52,46,78,52,65,45,25,23,49,50]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) print dummy_list" + ] + }, + { + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "42b07993-7f7a-4f5b-ec87-a3b891ba5839" + }, + "cell_type": "code", + "source": [ + "dummy_list=[1,52,46,78,52,65,45,25,23,49,50]\n", + "print(dummy_list)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 52, 46, 78, 52, 65, 45, 25, 23, 49, 50]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "9aeb6f4e-e962-4020-f2ba-3b7b1d2c3cbc" + }, + "cell_type": "code", + "source": [ + "dummy_list=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[50, 49, 23, 25, 45, 65, 52, 78, 46, 52, 1]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "1367ca9c-a701-4c08-f71d-e728cd688c6e" + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list_1=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "print (dummy_list)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 52, 46, 78, 52, 65, 45, 25, 23, 49, 50, 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": { + "base_uri": "https://localhost:8080/", + "height": 311 + }, + "outputId": "86173a15-e40a-4566-a544-4395658a17ae" + }, + "cell_type": "code", + "source": [ + "dummy_dict={}\n", + "dummy_dict[\"my_list\"]=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_dict[\"sub_dict\"]={\"key\": \"value\"}\n", + "dummy_dict[\"my_set\"]=set([1,52,46,78,52,65,45,25,23,49,50])\n", + "dummy_dict[(15.335,56.456)]=(\"Everest\",\"Nepal\",8356)\n", + "for key,value in dummy_dict.items():\n", + " print(\"key:{}\\nValue: {}\\n\\n\".format(key,value))\n" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "key:my_list\n", + "Value: [1, 52, 46, 78, 52, 65, 45, 25, 23, 49, 50]\n", + "\n", + "\n", + "key:sub_dict\n", + "Value: {'key': 'value'}\n", + "\n", + "\n", + "key:my_set\n", + "Value: {65, 1, 45, 46, 78, 49, 50, 52, 23, 25}\n", + "\n", + "\n", + "key:(15.335, 56.456)\n", + "Value: ('Everest', 'Nepal', 8356)\n", + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 311 + }, + "outputId": "b8fde1be-3953-4a00-af63-45fc3d719678" + }, + "cell_type": "code", + "source": [ + "dummy_dict={}\n", + "dummy_dict[\"my_list\"]=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_dict[\"sub_dict\"]={\"key\": \"value\"}\n", + "dummy_dict[\"my_set\"]=set([1,52,46,78,52,65,45,25,23,49,50])\n", + "dummy_dict[(15.335,56.456)]=(\"Everest\",\"Nepal\",8356)\n", + "for key,value in dummy_dict.items():\n", + " print(\"key:{}\\nValue: {}\\n\\n\".format(key,value))" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "key:my_list\n", + "Value: [1, 52, 46, 78, 52, 65, 45, 25, 23, 49, 50]\n", + "\n", + "\n", + "key:sub_dict\n", + "Value: {'key': 'value'}\n", + "\n", + "\n", + "key:my_set\n", + "Value: {65, 1, 45, 46, 78, 49, 50, 52, 23, 25}\n", + "\n", + "\n", + "key:(15.335, 56.456)\n", + "Value: ('Everest', 'Nepal', 8356)\n", + "\n", + "\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "d5300675-0fff-44f4-d28a-4f945c60a633" + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list_1=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "print (dummy_list)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 4, 9.45, 12.01, 12.02, 16, 23, 25, 45, 45.67, 46, 49, 50, 52, 52, 65, 78, 90, 200]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "3908c52b-87eb-4f2d-b458-e678719fecc1" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list_1=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "dummy_list.remove(x)\n", + "print (dummy_list)\n", + "# Let's play: try the same with something which is not in the list to get the ValueError" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 4, 9.45, 12.01, 12.02, 16, 23, 25, 45, 45.67, 46, 49, 50, 52, 52, 65, 78, 90]\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": "aMyo1gmRrVHo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "98b8d8b3-c8a8-40ae-b643-32e91bc9ac78" + }, + "cell_type": "code", + "source": [ + "# Ledummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "x=int(input(\"value\"))\n", + "dummy_list_1=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "dummy_list.remove(x)\n", + "print (dummy_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "value52\n", + "[0, 1, 1, 2, 4, 9.45, 12.01, 12.02, 16, 23, 25, 45, 45.67, 46, 49, 50, 52, 65, 78, 90, 200]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "metadata": { + "id": "qBC8lKpLrtJW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "b8e833a8-33ed-4b4a-cf54-0ff931afd97d" + }, + "cell_type": "code", + "source": [ + "dummy_list_1=[1,52,46,78,52,65,45,25,23,49,50]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "print (dummy_list)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 4, 9.45, 12.01, 12.02, 16, 23, 25, 45, 45.67, 46, 49, 50, 52, 52, 65, 78, 90, 200]\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..8946e3c --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,379 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Exercises.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MonojitBag/Assignment-2/blob/MonojitBag/Numpy_Exercises.ipynb)" + ] + }, + { + "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": { + "base_uri": "https://localhost:8080/", + "height": 219 + }, + "outputId": "f85e22cf-448a-4072-bdce-edb6416148f6" + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy\n", + "s=np.random.uniform(-1.3,2.5,64)\n", + "print(s)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-0.21060772 0.79312826 1.4734058 0.48079952 1.29843904 -0.96115442\n", + " -0.04434021 2.28235959 0.20789686 1.81820813 1.71206276 0.07943778\n", + " 2.46395034 0.6025707 -0.58656078 2.28309658 -0.13637224 -0.74144344\n", + " 1.11227751 1.87713202 2.14193442 -0.0099723 -0.63646606 0.03415162\n", + " 1.34034212 -0.97783931 0.75486278 0.85980073 0.15460385 2.43437119\n", + " 1.92758403 0.33248899 1.74054777 -0.56080518 1.92443702 0.03286954\n", + " 1.27740863 0.4849546 2.45805457 1.79206334 0.88642255 0.2943211\n", + " 0.01908824 1.36954157 0.80996875 0.76325266 0.49401712 1.49577459\n", + " 1.83106661 -0.34946721 0.55900233 0.62205716 -1.10765401 1.53224592\n", + " -0.03993473 0.10816507 -0.90543584 2.4737842 0.48199609 -0.59170816\n", + " -0.03464182 1.8829989 2.42661432 1.76294743]\n" + ], + "name": "stdout" + } + ] + }, + { + "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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "87a68a42-b4f1-451c-f98f-6f367ebc0fcc" + }, + "cell_type": "code", + "source": [ + "m=np.full(shape=(3,3),fill_value=[1,2,3])\n", + "print(m)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8027cb74-6507-4196-e253-c68752e3447e" + }, + "cell_type": "code", + "source": [ + "a=np.array([1,3,5,7,9])\n", + "print(a)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 3 5 7 9]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "bf81e986-6c28-4b30-ab5a-9dd8085808e0" + }, + "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", + "s=[val for val in a if val in b]\n", + "print(s)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 2, 4, 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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "9e3466ea-c415-4fd5-c31d-91a1ed4e1843" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "print(\"1d\",a)\n", + "a= np.arange(10).reshape(2,5)\n", + "print(\"2d\",a)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1d [0 1 2 3 4 5 6 7 8 9]\n", + "2d [[0 1 2 3 4]\n", + " [5 6 7 8 9]]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "94d9a239-0d55-4169-df2b-54bcf015c1c5" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "print(\"making list\",list(a))\n", + "a.reverse()\n", + "print(\"making list vice versa\",list(a))\n" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "making list [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "making list vice versa [9, 8, 7, 6, 5, 4, 3, 2, 1]\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", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 421 + }, + "outputId": "3bbc2997-5996-4722-fc4b-b0872239f1c0" + }, + "cell_type": "code", + "source": [ + "n=np.zeros(shape=(10,10))\n", + "print(\"original array\")\n", + "print(n)\n", + "n=np.ones(shape=(10,10))\n", + "print(\"frame with ones\")\n", + "n[1:-1,1:-1]=0\n", + "print(n)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "original array\n", + "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n", + "frame with ones\n", + "[[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": 182 + }, + "outputId": "a6f47cad-85a6-4f9d-c3ed-ce25e4a53c59" + }, + "cell_type": "code", + "source": [ + "x=np.ones((3,3))\n", + "print(\"checkerboard pattern:\")\n", + "x= np.zeros((8,8),dtype=int)\n", + "x[1::2,::2]=1\n", + "x[::2,1::2]=1\n", + "print(x)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "checkerboard pattern:\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", + " [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