From d43c0dd3fed0d7bb3031bde75c9477ce2b1c41d8 Mon Sep 17 00:00:00 2001 From: Sarith <68165093+sarithdm@users.noreply.github.com> Date: Fri, 11 Dec 2020 11:51:43 +0530 Subject: [PATCH] Update --- .../Case_study_data_structure_selection.ipynb | 1108 +++++++++++++++++ ...Bank.ipynb => Question_Bank_Module4.ipynb} | 54 +- .../Module_3_Question_Bank.ipynb | 298 +++++ 3 files changed, 1436 insertions(+), 24 deletions(-) create mode 100644 CST 283 Python for Machine Learning/Case_study_data_structure_selection.ipynb rename CST 283 Python for Machine Learning/Module 4/{Question_Bank.ipynb => Question_Bank_Module4.ipynb} (92%) create mode 100644 ITT 205 Problem Solving using Python/Question Bank/Module_3_Question_Bank.ipynb diff --git a/CST 283 Python for Machine Learning/Case_study_data_structure_selection.ipynb b/CST 283 Python for Machine Learning/Case_study_data_structure_selection.ipynb new file mode 100644 index 0000000..d879da5 --- /dev/null +++ b/CST 283 Python for Machine Learning/Case_study_data_structure_selection.ipynb @@ -0,0 +1,1108 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Case study: data structure selection", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "VqHO02GWM-Uy" + }, + "source": [ + "#Case study: data structure selection" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vZOxRLlEO8ME" + }, + "source": [ + "* How should you choose? The first step is to think about the operations you will need to implement for each data structure. \r\n", + "* Say If you need to add count of words. \r\n", + "* Your first choice might be a list, since it is easy to add elements, \r\n", + "* With tuples, you can’t append or remove, but you can use the addition operator to form a new tuple:\r\n", + "* You can use a dictionary if you need to store words as key and count as value\r\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kYJzIiUhR-JL" + }, + "source": [ + "### Other factors to consider in choosing data structures.\r\n", + "* One is run time. Sometimes there is a theoretical reason to expect one data structure to be faster than other; for example, I mentioned that the in operator is faster for dictionaries than for lists, at least when the number of elements is large.\r\n", + "\r\n", + "* But often you don’t know ahead of time which implementation will be faster. One option is to implement both of them and see which is better. This approach is called benchmarking. \r\n", + "* A practical alternative is to choose the data structure that is easiest to implement, and then see if it is fast enough for the intended application. \r\n", + "\r\n", + "* If so, there is no need to go on. If not, there are tools, like the profile module, that can identify the places in a program that take the most time.\r\n", + "\r\n", + "* The other factor to consider is storage space. In some cases, saving space can also make your program run faster, and in the extreme, your program might not run at all if you run out of memory. But for many applications, space is a secondary consideration after run time." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YxhHhmGpIcbF" + }, + "source": [ + "#13.1 Word frequency analysis" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-APtjFePNZbR" + }, + "source": [ + "f = open(\"mydata.dat\",\"w\")\r\n", + "f.write(\"apple and orange\\n\")\r\n", + "f.write(\"mango\\n\")\r\n", + "f.write(\"orange\\n\")\r\n", + "f.write(\"grapes\\n\")\r\n", + "f.close()" + ], + "execution_count": 63, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "q_meVzl9Q4Zx", + "outputId": "e3804307-581f-433a-c82d-7f318718461f" + }, + "source": [ + "f = open(\"mydata.dat\",\"r\")\r\n", + "text=f.read()\r\n", + "print(text)" + ], + "execution_count": 64, + "outputs": [ + { + "output_type": "stream", + "text": [ + "apple and orange\n", + "mango\n", + "orange\n", + "grapes\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6_jU1jMpUxmy", + "outputId": "44f108b8-bce6-4b0c-efee-6d8a75ccd904" + }, + "source": [ + "def freq(str):\r\n", + "\r\n", + " str = str.split() \r\n", + " print(str) \r\n", + "\r\n", + " str2 = [] \r\n", + "\r\n", + " for i in str: \r\n", + " #print(i) \r\n", + "\r\n", + " if i not in str2: \r\n", + " print(i)\r\n", + " print(str2)\r\n", + "\r\n", + " str2.append(i) \r\n", + "\r\n", + "\r\n", + "\r\n", + " for i in range(0, len(str2)): \r\n", + "\r\n", + " print('Frequency of', str2[i], 'is :', str.count(str2[i])) \r\n", + " print(str2) \r\n", + "\r\n", + "\r\n", + "str ='apple mango apple orange '\r\n", + "\r\n", + "freq(str) " + ], + "execution_count": 76, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['apple', 'mango', 'apple', 'orange']\n", + "apple\n", + "[]\n", + "mango\n", + "['apple']\n", + "orange\n", + "['apple', 'mango']\n", + "Frequency of apple is : 2\n", + "Frequency of mango is : 1\n", + "Frequency of orange is : 1\n", + "['apple', 'mango', 'orange']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "o4LMZd4-WgyV", + "outputId": "6c35259b-53ee-4fab-a7e0-8c0105f5812c" + }, + "source": [ + "f = open(\"mydata.dat\",\"w\")\r\n", + "f.write(\"apple and orange\\n\")\r\n", + "f.write(\"mango\\n\")\r\n", + "f.write(\"orange\\n\")\r\n", + "f.write(\"grapes\\n\")\r\n", + "f.close()\r\n", + "\r\n", + "f = open(\"mydata.dat\",\"r\")\r\n", + "text=f.read()\r\n", + "print(text)\r\n" + ], + "execution_count": 78, + "outputs": [ + { + "output_type": "stream", + "text": [ + "apple and orange\n", + "mango\n", + "orange\n", + "grapes\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YAO9iPizIc-C", + "outputId": "309bf475-97c2-4137-8a6a-8a0b94861504" + }, + "source": [ + "def hist(myfile):\r\n", + " hist={} #Empty\r\n", + " f1 = open(myfile, \"r\")\r\n", + " while True:\r\n", + " text = f1.readline() \r\n", + " if text == \"\":\r\n", + " break\r\n", + " words = text[:-1].split(\" \") \r\n", + " print(words)\r\n", + " for word in words:\r\n", + " if word in hist:\r\n", + " hist[word]=hist[word]+1 #incrementing\r\n", + " else:\r\n", + " hist[word]=1 # hist['apple']=1 {'apple':1,'and':1,'orange':2,'mango':1,'grapes':1}\r\n", + "\r\n", + " f1.close()\r\n", + " #print(hist)\r\n", + " return hist\r\n", + "\r\n", + "hist(\"mydata.dat\")" + ], + "execution_count": 82, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['apple', 'and', 'orange']\n", + "['mango']\n", + "['orange']\n", + "['grapes']\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 82 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7RIl5gtdIui4" + }, + "source": [ + "# 13.2 Random numbers" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EaA1MLLtIoFE", + "outputId": "4794433d-00c9-4b19-a3e4-d688d66ad8e9" + }, + "source": [ + "import random\r\n", + "\r\n", + "for i in range(10):\r\n", + " #print(i)\r\n", + " x = random.random()\r\n", + " print (x)" + ], + "execution_count": 84, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.7815285329571251\n", + "0.6705926499622238\n", + "0.056179322417355015\n", + "0.2238596618253167\n", + "0.7478502708139596\n", + "0.538598550035814\n", + "0.48886522778680086\n", + "0.8560787211603603\n", + "0.38217386300796996\n", + "0.19645380416417635\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b8DD0LubI6ow" + }, + "source": [ + "The function randint takes parameters low and high and returns an integer between low and high (including both)." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-zChxu39IzJp", + "outputId": "eaf7a90c-d7ac-4f1f-ceae-b59fc59a24d5" + }, + "source": [ + "random.randint(5, 100)" + ], + "execution_count": 88, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "45" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 88 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ULZotu7jJGTl", + "outputId": "edf8b066-aa23-438c-b6d6-a30f5834b934" + }, + "source": [ + "random.randint(5, 100)" + ], + "execution_count": 89, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "24" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 89 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ot1xaW4mJAZW" + }, + "source": [ + "To choose an element from a sequence at random, you can use choice:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "SFI3s0ZvI5Id", + "outputId": "2ea59a4b-bed0-49fd-d5a7-76caa5c03d9d" + }, + "source": [ + "t = [1, 2, 3,5,6]\r\n", + "t" + ], + "execution_count": 130, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 2, 3, 5, 6]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 130 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ORkL09_eJDcw", + "outputId": "b11904a8-aa2c-4390-d5e9-ddff51d33c22" + }, + "source": [ + " random.choice(t)" + ], + "execution_count": 131, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 131 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bF5HgVu9JDwQ", + "outputId": "ae1ca166-af78-4a91-85da-1319e9a65d54" + }, + "source": [ + "random.choice(t)" + ], + "execution_count": 95, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 95 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jQ1T16AzJMjI" + }, + "source": [ + "#13.3 Word histogram" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QtNU2opJJtfv" + }, + "source": [ + "f = open(\"mydata.dat\",\"w\")\r\n", + "f.write(\"apple and orange\\n\")\r\n", + "f.write(\"mango\\n\")\r\n", + "f.write(\"orange\\n\")\r\n", + "f.write(\"grapes\\n\")\r\n", + "f.close()" + ], + "execution_count": 11, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "lAu6-yfMJpXD" + }, + "source": [ + "def hist(myfile):\r\n", + " hist={}\r\n", + " f1 = open(myfile, \"r\")\r\n", + " while True:\r\n", + " text = f1.readline() \r\n", + " if text == \"\":\r\n", + " break\r\n", + " words = text[:-1].split(\" \") \r\n", + " #print(words)\r\n", + " for word in words:\r\n", + " if word in hist:\r\n", + " hist[word]=hist[word]+1\r\n", + " else:\r\n", + " hist[word]=1\r\n", + "\r\n", + " f1.close()\r\n", + " #print(hist)\r\n", + " return hist\r\n", + "\r\n", + "myhist =hist(\"mydata.dat\")" + ], + "execution_count": 98, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iyEcklCwZesz", + "outputId": "32a11e48-601e-4be5-badf-29550deb5f8c" + }, + "source": [ + "print(myhist)" + ], + "execution_count": 99, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'apple': 1, 'and': 1, 'orange': 2, 'mango': 1, 'grapes': 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ho5d5QCWJ6ln" + }, + "source": [ + "#13.4 Most common words" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BmeyhdJBNloh" + }, + "source": [ + "most_common takes a histogram and returns a list of word-frequency tuples, sorted in reverse order by frequency" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1wb6x2HjJZzG" + }, + "source": [ + "def most_common(hist):\r\n", + " t = []\r\n", + " for key, value in hist.items():\r\n", + " t.append((value, key))\r\n", + "\r\n", + " t.sort(reverse=True)\r\n", + " return t" + ], + "execution_count": 103, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MT_0Stu3KDxu", + "outputId": "94ad2b58-efef-4520-a066-64cda8028c40" + }, + "source": [ + "t = most_common(myhist)\r\n", + "print ('The most common words are:')\r\n", + "for freq, word in t[0:2]:\r\n", + " print (word, '\\t', freq)" + ], + "execution_count": 104, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The most common words are:\n", + "orange \t 2\n", + "mango \t 1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_1bf74eJK0ch" + }, + "source": [ + "#13.5 Optional parameters" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JZqusscUNrAY" + }, + "source": [ + "We have seen built-in functions and methods that take a variable number of arguments. It is possible to write user-defined functions with optional arguments, too. For example, here is a function that prints the most common words in a histogram" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YHcf6nThKMlV" + }, + "source": [ + "def print_most_common(hist, num=3):\r\n", + " t = most_common(hist)\r\n", + " print ('The most common words are:')\r\n", + " for freq, word in t[:num]:\r\n", + " print (word, '\\t', freq)" + ], + "execution_count": 112, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Vs0eppRcK7Wh", + "outputId": "797945ff-470f-4539-c925-4e55e2569dde" + }, + "source": [ + "print_most_common(myhist)" + ], + "execution_count": 113, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The most common words are:\n", + "orange \t 2\n", + "mango \t 1\n", + "grapes \t 1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JeCUuc1dLGAq", + "outputId": "67af2b8d-1b34-40b5-a07c-170dce70bafd" + }, + "source": [ + "print_most_common(myhist,5)" + ], + "execution_count": 114, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The most common words are:\n", + "orange \t 2\n", + "mango \t 1\n", + "grapes \t 1\n", + "apple \t 1\n", + "and \t 1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_xyahQA6auN9" + }, + "source": [ + "Write a function to calculate sum of n numbers\r\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TAArSK_Za0vA" + }, + "source": [ + "def sumofn(l=1,h=100):\r\n", + " pass" + ], + "execution_count": 111, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "00_2AmSNa-MC", + "outputId": "76047468-c867-405b-9f70-424d9ff12c65" + }, + "source": [ + "sumofn()" + ], + "execution_count": 110, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 110 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9LJo0QVNLXIQ" + }, + "source": [ + "#13.6 Dictionary subtraction" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XBrNkQOZNxXe" + }, + "source": [ + "subtract takes dictionaries d1 and d2 and returns a new dictionary that contains all the keys from d1 that are not in d2. Since we don’t really care about the values, we set them all to None." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "BW5UguydLyNs", + "outputId": "5cbf486a-e2cb-44f4-bd65-d48ca3176ec5" + }, + "source": [ + "f = open(\"mydata.dat\",\"w\")\r\n", + "f.write(\"apple and orange\\n\")\r\n", + "f.write(\"mango\\n\")\r\n", + "f.write(\"orange\\n\")\r\n", + "f.write(\"grapes\\n\")\r\n", + "f.write(\"banana\\n\")\r\n", + "f.close()\r\n", + "f = open(\"mydata.dat\",\"r\")\r\n", + "print(f.read())" + ], + "execution_count": 116, + "outputs": [ + { + "output_type": "stream", + "text": [ + "apple and orange\n", + "mango\n", + "orange\n", + "grapes\n", + "banana\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MY_7a5N_bsDK", + "outputId": "be23f489-4166-4f68-f2fb-9dadf2a1c40e" + }, + "source": [ + "words = hist('mydata.dat')\r\n", + "words" + ], + "execution_count": 118, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'banana': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 118 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "q4m8D1publzD", + "outputId": "5f6075c3-8e37-44bf-d962-e93fcc86a00d" + }, + "source": [ + "myhist" + ], + "execution_count": 117, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 117 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "z4fv5ZbsLLzS" + }, + "source": [ + "def subtract(d1, d2):\r\n", + " res = dict()\r\n", + " for key in d1:\r\n", + " #print(key)\r\n", + " if key not in d2:\r\n", + " res[key] = None\r\n", + " return res" + ], + "execution_count": 120, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "FUpKMXsbLcRe" + }, + "source": [ + "words = hist('mydata.dat')\r\n", + "diff = subtract(words, myhist)" + ], + "execution_count": 121, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wwl6PBWPMKzh", + "outputId": "7358c9b0-9992-4b9b-d2fa-2322630b573c" + }, + "source": [ + "words" + ], + "execution_count": 122, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'banana': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 122 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "A6i-13yfMM6d", + "outputId": "845c6c10-05ea-4357-83e2-96fab7e6b508" + }, + "source": [ + "myhist" + ], + "execution_count": 123, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 123 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "svPheB90LkPM", + "outputId": "eec7792a-9bf1-4e9c-90d9-7894f86f037a" + }, + "source": [ + "for word in diff.keys():\r\n", + " print (word)" + ], + "execution_count": 124, + "outputs": [ + { + "output_type": "stream", + "text": [ + "banana\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rGHuhWMFMvPY" + }, + "source": [ + "#13.7 Random words" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hHfdWBn6M5RI" + }, + "source": [ + "To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "412vFe3XcIU5", + "outputId": "9be5d8c5-b664-4ce6-c33d-0bf1d4ccf2c4" + }, + "source": [ + "myhist" + ], + "execution_count": 125, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'and': 1, 'apple': 1, 'grapes': 1, 'mango': 1, 'orange': 2}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 125 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "zETs9569cSCF", + "outputId": "e6bed4ee-edb7-4381-a712-34421588a1a8" + }, + "source": [ + "myhist.items()" + ], + "execution_count": 126, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_items([('apple', 1), ('and', 1), ('orange', 2), ('mango', 1), ('grapes', 1)])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 126 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ueab6mFZML_E" + }, + "source": [ + "def random_word(h):\r\n", + " t = []\r\n", + " for word, freq in h.items():\r\n", + " t.extend([word] * freq*2) \r\n", + " #print(t)\r\n", + "\r\n", + " return random.choice(t)" + ], + "execution_count": 146, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "dieweu23Lqi3", + "outputId": "b585ed78-e25e-4628-c1c6-40ba0eec918e" + }, + "source": [ + "random_word(myhist)" + ], + "execution_count": 147, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'and'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 147 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ONHIwP9lM2T0" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XpK1Xx4pOp0g" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/CST 283 Python for Machine Learning/Module 4/Question_Bank.ipynb b/CST 283 Python for Machine Learning/Module 4/Question_Bank_Module4.ipynb similarity index 92% rename from CST 283 Python for Machine Learning/Module 4/Question_Bank.ipynb rename to CST 283 Python for Machine Learning/Module 4/Question_Bank_Module4.ipynb index e46eb61..2f55adb 100644 --- a/CST 283 Python for Machine Learning/Module 4/Question_Bank.ipynb +++ b/CST 283 Python for Machine Learning/Module 4/Question_Bank_Module4.ipynb @@ -79,7 +79,7 @@ "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "73a4242e-d0eb-427c-91b7-5a8466101228" + "outputId": "8a80ce00-7484-4282-89b0-efa0813893c1" }, "source": [ "a = Employee(\"Development\",\"Devika\",28,33333,\"ABC\",43000)\n", @@ -133,7 +133,7 @@ "base_uri": "https://localhost:8080/" }, "id": "hll_VzGp6wAr", - "outputId": "fede0bd5-03dc-4a40-df12-c4212c90b193" + "outputId": "8c268d48-1587-4c7f-abb2-2e3ecddbe0ee" }, "source": [ "class Student(object): \n", @@ -144,7 +144,7 @@ " print(\"Student Class\") \n", "\n", " def accept_details(self):\n", - " #self.name=input(\"ENter Name\")\n", + " self.name=input(\"ENter Name\")\n", " pass\n", " def display_details(self):\n", " pass\n", @@ -166,22 +166,25 @@ " print(\"Statement Class\") \n", " def accept_mark(self):\n", " Student.accept_details(self)\n", - " Student.display_details() #\n", + " #Student.display_details() #\n", " self.m1 = int(input(\"Sub 1 \"))\n", " self.m2 = int(input(\"Sub 2 \"))\n", " self.m3 = int(input(\"Sub 3\"))\n", " Sports.accept_sportmark(self)\n", - " Sports.display_mark() #\n", + " #Sports.display_mark() #\n", "\n", "\n", " def display_total(self):\n", " self.Total = self.m1+self.m2+self.m3+self.smark\n", - " print(\"Name\")\n", + " #print(\"Name\")\n", + " print(\"Total Marks\",self.Total)\n", " \t\t\n", "\n", "ob = Statement() \n", - "ob.accept_mark() \n", - "ob.display_total() \n" + "ob.accept_mark()\n", + "ob.display_total()\n", + "\n", + "\n" ], "execution_count": null, "outputs": [ @@ -192,10 +195,10 @@ "Sports Class\n", "Statement Class\n", "Sub 1 10\n", - "Sub 2 10\n", - "Sub 310\n", - "Sport Mark 100\n", - "ABC's Total marks is 130\n" + "Sub 2 20\n", + "Sub 330\n", + "Sport Mark 90\n", + "Total Marks 150\n" ], "name": "stdout" } @@ -217,10 +220,11 @@ "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "73ab4ad9-d3a5-413e-a4fd-0677ce9ddddb" + "outputId": "6cb2e655-0ac4-42cb-edaf-d8f824988995" }, "source": [ " \"\"\"@Author Ameeka Nazreen\"\"\"\n", + " import math\n", " class Singlenumber:\n", " def __init__(self):\n", " self.intnum=0\n", @@ -277,13 +281,13 @@ "base_uri": "https://localhost:8080/" }, "id": "Mfo2qz9p6Syp", - "outputId": "969ceaf4-59f9-4cef-c96c-35cc7584cf82" + "outputId": "c1ff5130-ed65-473e-c3e1-7769ca96b83a" }, "source": [ "#@Sruthi Viswanathan\n", "class Polygon:\n", " def __init__(self,number,name):\n", - " self.number = number\n", + " self.number = number #3 - Triangle\n", " self.name=name\n", " self.sides=[]\n", " def inputsides(self):\n", @@ -304,7 +308,7 @@ "class Square(Polygon):\n", "\n", " def __init__(self):\n", - " Polygon.__init__(self, 2, \"SQUARE :\")\n", + " Polygon.__init__(self, 4, \"SQUARE :\")\n", "\n", " def inputsides(self):\n", " print(self.name)\n", @@ -332,9 +336,9 @@ "text": [ "TRIANGLE :\n", "Enter side 1 : 10\n", - "Enter side 2 : 10\n", - "Enter side 3 : 10\n", - "The area of the triangle is : 43.30 \n", + "Enter side 2 : 20\n", + "Enter side 3 : 30\n", + "The area of the triangle is : 0.00 \n", "SQUARE :\n", "Enter side : 10\n", "The area of square is : 100.00 \n" @@ -405,7 +409,7 @@ "base_uri": "https://localhost:8080/" }, "id": "Ea3xqMntUNmS", - "outputId": "fb3ae3d0-638d-48d2-97c9-39a2acc52979" + "outputId": "51198c3b-0f95-49f7-f21b-cad2c0eb1c43" }, "source": [ "class Rectange:\n", @@ -431,7 +435,7 @@ " def perimeter(self):\n", " print(\"Perimeter:\", 2*(self.height+self.width))\n", "\n", - "robj = Rectange()\n", + "robj = Rectange(10,5)\n", "\n", "robj.center()\n", "robj.area()\n", @@ -443,9 +447,11 @@ { "output_type": "stream", "text": [ - "Center: (5 , 10)\n", - "Area: 200\n", - "Perimeter: 60\n" + "Enter x_c0\n", + "Enter y_c0\n", + "Center: (5 , 2.5)\n", + "Area: 50\n", + "Perimeter: 30\n" ], "name": "stdout" } diff --git a/ITT 205 Problem Solving using Python/Question Bank/Module_3_Question_Bank.ipynb b/ITT 205 Problem Solving using Python/Question Bank/Module_3_Question_Bank.ipynb new file mode 100644 index 0000000..d42c9df --- /dev/null +++ b/ITT 205 Problem Solving using Python/Question Bank/Module_3_Question_Bank.ipynb @@ -0,0 +1,298 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Module 3 Question Bank", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "qdGDE7DvW7Rv" + }, + "source": [ + "# Write a Python program to create a lambda function that adds 15 to a given number passed in as an argument, also create a lambda function that multiplies argument x with argument y and print the result\r\n", + "\r\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4KKXeZkfW8BX" + }, + "source": [ + "add15 = lambda x: x + 15\r\n" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "O8anLeEkXRlk", + "outputId": "48ed4b91-1327-4a3d-a574-765570f7a26d" + }, + "source": [ + "add15(10)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "25" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mu_qxqxZXW3o" + }, + "source": [ + "mulxy = lambda x,y: x*y\r\n" + ], + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Ji9hxTyDXjFU", + "outputId": "5f81e3fb-f831-4159-9b4a-3bb6953e6295" + }, + "source": [ + "mulxy(10,2)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "20" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 8 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BlXa-7sTXncG" + }, + "source": [ + "Write a recursive Python function that has a \r\n", + "parameter representing a list of integers and returns the maximum \r\n", + "stored in the list. Thinking recursively, the maximum is either the \r\n", + "first value in the list or the maximum of the rest of the list, \r\n", + "whichever is larger. If the list only has 1 integer, then its maximum \r\n", + "is this single value, naturally\r\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dFeeVUq_XkyB" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qEaMmzhtb6I_" + }, + "source": [ + "# Write a python program using list to store and display the average of N Integers accepted from the user\r\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "olpL0pMab8ss", + "outputId": "c1d31b56-f482-4071-95e8-fff3843039f0" + }, + "source": [ + "def avg_list(items):\r\n", + " sum = 0\r\n", + " for x in items:\r\n", + " sum += x\r\n", + " avg=sum/len(items)\r\n", + " return avg\r\n", + "\r\n", + "\r\n", + "print(avg_list([1,2,4,8]))\r\n" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3.75\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iUq2pKd1ewv5" + }, + "source": [ + "Write a Python program to create a list by concatenating a given list which range goes from 1 to n. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6-f13KmXefS9" + }, + "source": [ + "# Write a Python program to compute the nth Fibonacci number . Use a recursive function for the implementation." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WSJH6DZKejPt" + }, + "source": [ + "def fib(n):\r\n", + " if n <= 1:\r\n", + " return n\r\n", + " else:\r\n", + " return(fib(n-1) + fib(n-2))" + ], + "execution_count": 20, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vKsvvTE7ct1P" + }, + "source": [ + "# Write a recursive function to calculate the sum of numbers from 0 to 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lFXRG1b0cXR7", + "outputId": "4f822062-6a9c-4277-f6ae-b6efcdbe557e" + }, + "source": [ + "def sum(lower,upper):\r\n", + " if lower>upper:\r\n", + " return 0\r\n", + " else:\r\n", + " #print(lower)\r\n", + " return lower + sum(lower+1,upper)\r\n", + "sum(0,10)" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "55" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 12 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TeG6c9w5c8-T" + }, + "source": [ + "#Write a recursive function to find the factorial of a number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9TguB-6qc3Lf", + "outputId": "0ecc66ab-c92e-4fd8-826d-b38d0e320be6" + }, + "source": [ + "def fact(n):\r\n", + " if n == 1:\r\n", + " return n\r\n", + " else:\r\n", + " return(n*fact(n-1))\r\n", + "fact(4)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "24" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oOTJ7BX1dNCs" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file