{"nbformat_minor": 0, "cells": [{"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "n=2 #number of employees\nr=12 #number of office rooms\nways_alloc_sanchez=12\nways_alloc_patel=11\n\n#By PRODUCT RULE\nprint \"Total ways to assign offices to these employees is\",ways_alloc_sanchez*ways_alloc_patel\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total ways to assign offices to these employees is 132\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "alphabets=26 #Total number of alphabets\nposint=100 #Total positive numbers not beyond 100\n\n#number of chairs to be labelled with a alphabet and an integer using product rule\nprint \"Total number of chairs that can be labelled with an alphabet and an integer is\",alphabets*posint\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total number of chairs that can be labelled with an alphabet and an integer is 2600\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "mc=32 #total number of microcomputers\nport=24 #total number of ports in each microcomputer\n\n#total number of different ports to a microcomputer in the center are found using product rule\n\nprint \"total number of ports\",mc*port\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "total number of ports 768\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "bits=2 #possible bits either 0 or 1\nns=7 #number of bits in the string (ie). length of the string\n # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE\nprint \"Total different bit strings of lenth seven are\",bits**ns\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total different bit strings of lenth seven are 128\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 387", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "letters=26 #number of letters in english alphabet\nno_of_letters=3 #number of letters \nchoices=10 #number of choices for each letter\nresult=1#in order to avoid junk values. Assigned it to 1.\nfor i in range(0,no_of_letters):\n result=result*letters*choices\nprint \"The total number of choices are\",result", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The total number of choices are 17576000\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 407", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #computing the permutation\n result=result*i\n \n return result\n\nprint \"The number of ways to select 3 students from a group of 5 students to line up for a picture is \",permutation(5,3) #function call\nprint \"The number of ways to select 5 students from a group of 5 students to line up for a picture is \",permutation(5,5) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The number of ways to select 3 students from a group of 5 students to line up for a picture is 60\nThe number of ways to select 5 students from a group of 5 students to line up for a picture is 120\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #permutation computation\n result=result*i\n return result\nnum=input(\"Enter the number of people\")\nperm=input(\"Enter the prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of people100\nEnter the prizes3\nThe number of ways to decide the prize winners is 970200\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "def permutation(n,r):\n \n i=n\n result=1\n for i in range((n-r)+1,n+1):\n result=result*i\n \n return result\nnum=input(\"Enter the number of runners\")\nperm=input(\"Enter the number of prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of runners8\nEnter the number of prizes3\nThe number of ways to decide the prize winners is 336\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "def calc(n):\n \n i=n\n result=1\n for i in range(1,n): #find the number of ways to decide the path. since the first city us decided. The for loop is from 1 to n\n result=result*i\n \n return result\nnum=input(\"Enter the number of cities\") \nprint \"The number of possible ways to decide the path is\",calc(num)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of cities8\nThe number of possible ways to decide the path is 5040\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 410", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "def combination(n,r): #combination function\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):#computes the value of the numerator \n numerator=numerator*i\n for j in range (1,r+1): #computes the value of the denominator\n denominator=denominator*j\n result=numerator/denominator #computes result\n return result\nnum=input(\"Enter the number of elements\")\ncomb=input(\"Enter the combinations\")\nprint \"The number of combinations are \",combination(num,comb)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of elements4\nEnter the combinations2\nThe number of combinations are 6\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 12: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "def combination(n,r): #function definition for combination\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the number of members in a team\")\ncomb=input(\"Enter the number of players\")\nprint \"The number of combinations are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of members in a team10\nEnter the number of players5\nThe number of combinations are 252\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 13: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 8, "cell_type": "code", "source": "def combination(n,r): #function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the total number of astronauts\")\ncomb=input(\"Enter the number of astronauts to be selected \")\nprint \"The total number of combinations of selected astronauts to Mars are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of astronauts30\nEnter the number of astronauts to be selected 6\nThe total number of combinations of selected astronauts to Mars are 593775\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 15: Page 413", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "def combination(n,r): #Function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1): #computation of the numerator\n numerator=numerator*i\n for j in range (1,r+1): #computation of the denominator\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum1=input(\"Enter the total number of faculty in computer science department\")\ncomb1=input(\"Enter the number of faculty to be selected for computer science department\")\nnum2=input(\"Enter the total number of faculty in maths department\")\ncomb2=input(\"Enter the number of faculty to be selected for maths department\")\n\nprint \"The total number of combinations of selected faculties are \",combination(num1,comb1)*combination(num2,comb2) #Function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of faculty in computer science department9\nEnter the number of faculty to be selected for computer science department3\nEnter the total number of faculty in maths department11\nEnter the number of faculty to be selected for maths department4\nThe total number of combinations of selected faculties are 27720\n"}], "metadata": {"collapsed": false, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}