Chapter 06: Counting

Example 01: Page 386

In [1]:
n=2 #number of employees
r=12 #number of office rooms
ways_alloc_sanchez=12
ways_alloc_patel=11

#By PRODUCT RULE
print "Total ways to assign offices to these employees is",ways_alloc_sanchez*ways_alloc_patel
Total ways to assign offices to these employees is 132

Example 02: Page 386

In [2]:
alphabets=26 #Total number of alphabets
posint=100 #Total positive numbers not beyond 100

#number of chairs to be labelled with a alphabet and an integer using product rule
print "Total number of chairs that can be labelled with an alphabet and an integer is",alphabets*posint
Total number of chairs that can be labelled with an alphabet and an integer is 2600

Example 03: Page 386

In [3]:
mc=32 #total number of microcomputers
port=24 #total number of ports in each microcomputer

#total number of different ports to a microcomputer in the center are found using product rule

print "total number of ports",mc*port
total number of ports 768

Example 04: Page 386

In [4]:
bits=2 #possible bits either 0 or 1
ns=7 #number of bits in the string (ie). length of the string
 # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE
print "Total different bit strings of lenth seven are",bits**ns
Total different bit strings of lenth seven are 128

Example 05: Page 387

In [7]:
letters=26 #number of letters in english alphabet
no_of_letters=3 #number of letters 
choices=10 #number of choices for each letter
result=1#in order to avoid junk values.  Assigned it to 1.
for i in range(0,no_of_letters):
    result=result*letters*choices
print "The total number of choices are",result
The total number of choices are 17576000

Example 01: Page 407

In [10]:
def permutation(n,r): #function definition
    
    i=n
    result=1
    for i in range((n-r)+1,n+1): #computing the permutation
       result=result*i
    
    return result

print "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
print "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
The number of ways to select 3 students from a group of 5 students to line up for a picture is  60
The number of ways to select 5 students from a group of 5 students to line up for a picture is  120

Example 04: Page 409

In [1]:
def permutation(n,r): #function definition
    
    i=n
    result=1
    for i in range((n-r)+1,n+1): #permutation computation
       result=result*i
    return result
num=input("Enter the number of people")
perm=input("Enter the prizes")
print "The number of ways to decide the prize winners is",permutation(num,perm) #function call
Enter the number of people100
Enter the prizes3
The number of ways to decide the prize winners is 970200

Example 05: Page 409

In [2]:
def permutation(n,r):
    
    i=n
    result=1
    for i in range((n-r)+1,n+1):
       result=result*i
    
    return result
num=input("Enter the number of runners")
perm=input("Enter the number of prizes")
print "The number of ways to decide the prize winners is",permutation(num,perm)
Enter the number of runners8
Enter the number of prizes3
The number of ways to decide the prize winners is 336

Example 06: Page 409

In [3]:
def calc(n):
    
    i=n
    result=1
    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
      result=result*i
    
    return result
num=input("Enter the number of cities") 
print "The number of possible ways to decide the path is",calc(num)
Enter the number of cities8
The number of possible ways to decide the path is 5040

Example 10: Page 410

In [5]:
def combination(n,r): #combination function
    i=n
    numerator=1
    denominator=1
    for i in range((n-r)+1,n+1):#computes the value of the numerator 
       numerator=numerator*i
    for j in range (1,r+1): #computes the value of the denominator
        denominator=denominator*j
    result=numerator/denominator #computes result
    return result
num=input("Enter the number of elements")
comb=input("Enter the combinations")
print "The number of combinations are ",combination(num,comb)
Enter the number of elements4
Enter the combinations2
The number of combinations are  6

Example 12: Page 412

In [7]:
def combination(n,r): #function definition for combination
    i=n
    numerator=1
    denominator=1
    for i in range((n-r)+1,n+1):
       numerator=numerator*i
    for j in range (1,r+1):
        denominator=denominator*j
    result=numerator/denominator
    return result
num=input("Enter the number of members in a team")
comb=input("Enter the number of players")
print "The number of combinations are ",combination(num,comb) #function call
Enter the number of members in a team10
Enter the number of players5
The number of combinations are  252

Example 13: Page 412

In [8]:
def combination(n,r): #function definition
    i=n
    numerator=1
    denominator=1
    for i in range((n-r)+1,n+1):
       numerator=numerator*i
    for j in range (1,r+1):
        denominator=denominator*j
    result=numerator/denominator
    return result
num=input("Enter the total number of astronauts")
comb=input("Enter the number of astronauts to be selected ")
print "The total number of combinations of selected astronauts to Mars are ",combination(num,comb) #function call
Enter the total number of astronauts30
Enter the number of astronauts to be selected 6
The total number of combinations of selected astronauts to Mars are  593775

Example 15: Page 413

In [9]:
def combination(n,r): #Function definition
    i=n
    numerator=1
    denominator=1
    for i in range((n-r)+1,n+1): #computation of the numerator
       numerator=numerator*i
    for j in range (1,r+1): #computation of the denominator
        denominator=denominator*j
    result=numerator/denominator
    return result
num1=input("Enter the total number of faculty in computer science department")
comb1=input("Enter the number of faculty to be selected for computer science department")
num2=input("Enter the total number of faculty in maths department")
comb2=input("Enter the number of faculty to be selected for maths department")

print "The total number of combinations of selected faculties are ",combination(num1,comb1)*combination(num2,comb2) #Function call
Enter the total number of faculty in computer science department9
Enter the number of faculty to be selected for computer science department3
Enter the total number of faculty in maths department11
Enter the number of faculty to be selected for maths department4
The total number of combinations of selected faculties are  27720