From dd5987fbeb1e6c411d6ba2434bd9a7e59af9c2ff Mon Sep 17 00:00:00 2001 From: Hrishabh Kumar Date: Thu, 5 Mar 2020 18:46:22 +0530 Subject: [PATCH 1/3] Problem Solved ! (by Hrishabh) --- Questions/Excercise numpy .ipynb | 373 ++++++++++++++++++++++++++++++- 1 file changed, 372 insertions(+), 1 deletion(-) diff --git a/Questions/Excercise numpy .ipynb b/Questions/Excercise numpy .ipynb index 92ed880..ad03d74 100644 --- a/Questions/Excercise numpy .ipynb +++ b/Questions/Excercise numpy .ipynb @@ -1 +1,372 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Excercise numpy .ipynb","provenance":[],"authorship_tag":"ABX9TyMEuYnB4Fa7QF811lAB6GtK"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"a_4UupTr9fbX","colab_type":"text"},"source":["# Numpy Exercises\n","\n","1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions"]},{"cell_type":"code","metadata":{"id":"LIP5u4zi0Nmg","colab_type":"code","colab":{}},"source":["import numpy as np #import numpy"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"dBoH_A7M9jjL","colab_type":"text"},"source":["2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3"]},{"cell_type":"code","metadata":{"id":"4TxT66309n1o","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Vh-UKizx9oTp","colab_type":"text"},"source":["3) Create an array of the first 10 odd integers."]},{"cell_type":"code","metadata":{"id":"ebhEUZq29r32","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QfJRdMat90f4","colab_type":"text"},"source":["4) Find intersection of a and b"]},{"cell_type":"code","metadata":{"id":"gOlfuJCo-JwF","colab_type":"code","colab":{}},"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])"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"RtVCf0UoCeB8","colab_type":"text"},"source":["5) Reshape 1d array a to 2d array of 2X5"]},{"cell_type":"code","metadata":{"id":"2E8b55_2Cjx5","colab_type":"code","colab":{}},"source":["a = np.arange(10)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"dVrSBW1zEjp2","colab_type":"text"},"source":["6) Create a numpy array to list and vice versa"]},{"cell_type":"code","metadata":{"id":"tcBCyhXPEp9C","colab_type":"code","colab":{}},"source":["a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"JNqX8wnz9sQJ","colab_type":"text"},"source":["7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones."]},{"cell_type":"code","metadata":{"id":"4bjP3JAc9vRD","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"xaQgf8tT9v-n","colab_type":"text"},"source":["8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach."]},{"cell_type":"code","metadata":{"id":"No7fx0Xy9zEh","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Excercise numpy .ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "source": [ + "# Numpy Exercises\n", + "\n", + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LIP5u4zi0Nmg", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 202 + }, + "outputId": "1c46fad6-6eec-4e37-897d-bc077dc300b2" + }, + "source": [ + "import numpy as np #import numpy\n", + "\n", + "a = np.linspace(-1.3,2.5, num=64)\n", + "print(a)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-1.3 -1.23968254 -1.17936508 -1.11904762 -1.05873016 -0.9984127\n", + " -0.93809524 -0.87777778 -0.81746032 -0.75714286 -0.6968254 -0.63650794\n", + " -0.57619048 -0.51587302 -0.45555556 -0.3952381 -0.33492063 -0.27460317\n", + " -0.21428571 -0.15396825 -0.09365079 -0.03333333 0.02698413 0.08730159\n", + " 0.14761905 0.20793651 0.26825397 0.32857143 0.38888889 0.44920635\n", + " 0.50952381 0.56984127 0.63015873 0.69047619 0.75079365 0.81111111\n", + " 0.87142857 0.93174603 0.99206349 1.05238095 1.11269841 1.17301587\n", + " 1.23333333 1.29365079 1.35396825 1.41428571 1.47460317 1.53492063\n", + " 1.5952381 1.65555556 1.71587302 1.77619048 1.83650794 1.8968254\n", + " 1.95714286 2.01746032 2.07777778 2.13809524 2.1984127 2.25873016\n", + " 2.31904762 2.37936508 2.43968254 2.5 ]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4TxT66309n1o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "51627d70-efe9-4793-81a4-ff7f2d916522" + }, + "source": [ + "a = np.array([1,2,3])\n", + "n = input(\"Enter value of n: \")\n", + "b = np.resize(a,(3*int(n)))\n", + "print(b)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter value of n: 4\n", + "[1 2 3 1 2 3 1 2 3 1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ebhEUZq29r32", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "bbd02d4d-6a12-491a-a73d-d99e11e5fe2b" + }, + "source": [ + "a = np.arange(20)\n", + "index = a%2!= 0\n", + "print(a[index])" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17 19]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gOlfuJCo-JwF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "3f9290c2-7738-4eb2-d5a5-f995fdae55be" + }, + "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", + "np.intersect1d(a,b)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 4])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2E8b55_2Cjx5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "a5a8593b-c213-46ee-bb3f-74e5d0947713" + }, + "source": [ + "a = np.arange(10)\n", + "a = a.reshape(2,5)\n", + "print(a)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tcBCyhXPEp9C", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "3be6c93b-05c9-48cc-fb3f-172b4d03d25f" + }, + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "print(type(a)) # Prints \"\"\n", + "a = np.asarray(a)\n", + "print(type(a)) # Prints\"\"\n", + "a = a.tolist();\n", + "print(type(a)) # Prints \"\"" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JNqX8wnz9sQJ", + "colab_type": "text" + }, + "source": [ + "7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4bjP3JAc9vRD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 185 + }, + "outputId": "f983b76b-f48d-46ed-893a-27ba13578e55" + }, + "source": [ + "arrayOfZeros = np.zeros((10,10))\n", + "arrayOfZeros[0,] = 1\n", + "arrayOfZeros[9:,] = 1\n", + "arrayOfZeros[:,0] = 1\n", + "arrayOfZeros[:,9:] = 1\n", + "print(arrayOfZeros)\n" + ], + "execution_count": 8, + "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" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xaQgf8tT9v-n", + "colab_type": "text" + }, + "source": [ + "8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "No7fx0Xy9zEh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 151 + }, + "outputId": "fa9bd1ba-ceb2-4b34-bfae-112e73084f2d" + }, + "source": [ + "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": [ + "[[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" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "uLzHTtXh1HfI", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 93598b2bf4e1a7c46444579a5d8fc2e4a27c60d9 Mon Sep 17 00:00:00 2001 From: Hrishabh Kumar Date: Thu, 5 Mar 2020 19:32:26 +0530 Subject: [PATCH 2/3] Problem Solved ! (by Hrishabh) --- Questions/List 1.ipynb | 403 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 402 insertions(+), 1 deletion(-) diff --git a/Questions/List 1.ipynb b/Questions/List 1.ipynb index 144d52a..10ff15b 100644 --- a/Questions/List 1.ipynb +++ b/Questions/List 1.ipynb @@ -1 +1,402 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"List 1.ipynb","provenance":[],"authorship_tag":"ABX9TyNLahDgX6PyIABRXbqfMC/+"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"e0R1W0Vzm4UU","colab_type":"text"},"source":["# Exercise - List"]},{"cell_type":"markdown","metadata":{"id":"TrO7XNQnnQZ7","colab_type":"text"},"source":["1) Create any random list and assign it to a variable dummy_list"]},{"cell_type":"code","metadata":{"id":"bjl-2QkznWid","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"cDjddNGfngnp","colab_type":"text"},"source":["2) print dummy_list"]},{"cell_type":"code","metadata":{"id":"RVL5178inz9M","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"15jKDXxkn16M","colab_type":"text"},"source":["3) Reverse dummy_list and print"]},{"cell_type":"code","metadata":{"id":"bYa9gFOOn-4o","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"EShv0nfXpUys","colab_type":"text"},"source":["4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list"]},{"cell_type":"code","metadata":{"id":"Ngkc7hnYphg6","colab_type":"code","colab":{}},"source":["dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Le1aRTuYoDzS","colab_type":"text"},"source":["5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. "]},{"cell_type":"code","metadata":{"id":"VHfSR_Csthnk","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"rtPC6WkSos-y","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"RgCYpFXGou6q","colab_type":"text"},"source":["6) print dummy_dict"]},{"cell_type":"code","metadata":{"id":"qe5E5IgxpTWU","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"8n_nsBDup4--","colab_type":"text"},"source":["7) Sort dummy_list in ascending order as well as descending order and print the changed lists "]},{"cell_type":"code","metadata":{"id":"Z_m7vr26qKnK","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Znm5Qo4LqPKA","colab_type":"text"},"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."]},{"cell_type":"code","metadata":{"id":"1-8mlngDqYvS","colab_type":"code","colab":{}},"source":["x = 200\n","\n","# Let's play: try the same with something which is not in the list to get the ValueError"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QPB6iGbeqviN","colab_type":"text"},"source":["9) Remove the item at position x. x is any random integer"]},{"cell_type":"code","metadata":{"id":"aMyo1gmRrVHo","colab_type":"code","colab":{}},"source":["# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"bqQnnsr8rm6G","colab_type":"text"},"source":["10) Let's clean everything clear the list and then print"]},{"cell_type":"code","metadata":{"id":"qBC8lKpLrtJW","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "List 1.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "e0R1W0Vzm4UU", + "colab_type": "text" + }, + "source": [ + "# Exercise - List" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TrO7XNQnnQZ7", + "colab_type": "text" + }, + "source": [ + "1) Create any random list and assign it to a variable dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bjl-2QkznWid", + "colab_type": "code", + "colab": {} + }, + "source": [ + "list = [1,2,3,'aa','ddd']\n", + "dummy_list = list" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "source": [ + "2) print dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "dd41ef67-6f94-42e4-83b3-1e73116d535f" + }, + "source": [ + "print(dummy_list)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 3, 'aa', 'ddd']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bYa9gFOOn-4o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "d0d3ddff-7734-484a-bab5-c3fd94c129e3" + }, + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['ddd', 'aa', 3, 2, 1]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EShv0nfXpUys", + "colab_type": "text" + }, + "source": [ + "4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ngkc7hnYphg6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "0066a7da-8227-4870-8d20-c75a9ebbd0c4" + }, + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "\n", + "dummy_list.extend(dummy_list_2)\n", + "print(dummy_list)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['ddd', 'aa', 3, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Le1aRTuYoDzS", + "colab_type": "text" + }, + "source": [ + "5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rtPC6WkSos-y", + "colab_type": "code", + "colab": {} + }, + "source": [ + "dummy_dict = {}\n", + "\n", + "for i in range(len(dummy_list)):\n", + " dummy_dict[dummy_list[i]] = dummy_list.count(dummy_list[i])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "source": [ + "6) print dummy_dict" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qe5E5IgxpTWU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "d5f49385-24af-42fb-d454-277ad05ab920" + }, + "source": [ + "print(dummy_dict)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'ddd': 1, 'aa': 1, 3: 1, 2: 2, 1: 2, 200: 1, 16: 1, 4: 1, 0: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8n_nsBDup4--", + "colab_type": "text" + }, + "source": [ + "7) Sort dummy_list in ascending order as well as descending order and print the changed lists " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z_m7vr26qKnK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "1fb2ec5b-b115-42df-c3be-3392c67563b8" + }, + "source": [ + "dummy_list = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "\n", + "dummy_list.sort()\n", + "print(dummy_list)\n", + "\n", + "dummy_list.sort(reverse=True)\n", + "print(dummy_list)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 45.67, 90, 200]\n", + "[200, 90, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Znm5Qo4LqPKA", + "colab_type": "text" + }, + "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." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1-8mlngDqYvS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 195 + }, + "outputId": "c0c56ca6-ea56-45f4-f0f1-7817d233ed4e" + }, + "source": [ + "x = 200\n", + "\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "dummy_list.remove(x)" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "error", + "ename": "ValueError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Let's play: try the same with something which is not in the list to get the ValueError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aMyo1gmRrVHo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 195 + }, + "outputId": "567be961-5c61-40a7-fefc-7949b42b7597" + }, + "source": [ + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "\n", + "len(dummy_list) # Prints\"10\"\n", + "\n", + "pos = 12\n", + "dummy_list.remove(pos)" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "error", + "ename": "ValueError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mpos\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpos\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qBC8lKpLrtJW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1f0851d8-be9d-4d33-c2e6-1d203f942272" + }, + "source": [ + "dummy_list.clear()\n", + "dummy_list" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qSByX5MnAB-l", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 4eb929246ea699eee92a5c68d15d3df8eff1daad Mon Sep 17 00:00:00 2001 From: Hrishabh Kumar Date: Thu, 5 Mar 2020 20:04:20 +0530 Subject: [PATCH 3/3] List Problem Solved ! (by Hrishabh)