Friday 5 June 2015

Itertool module in python

#Nadia baig

import itertools

seq=input("Enter sequence to apply itertool functions\n")

print(seq)

# 1Applying product rule

def pro(seq,repeat=2):

 return (list (itertools.product(seq, repeat=2)))
p=pro(seq,repeat=2)

#2Applying  permutation

def per(seq,rep=2):
 return ( list(itertools.permutations(seq, 2)))
q=per(seq,2)

#3Applying combination
def com(seq,rep=2):
    return (list(itertools.combinations(seq,2)))
c=com(seq,2)

#4 Applying  combinations with replacement
def comr(seq,rep=2):
    return (list(itertools.combinations_with_replacement(seq,2)))
d=comr(seq,2)

#Menu
choice="please select any choice to perform following operations\n  to apply product rule press 1\n press 2 to perform permutatino\n press 3 to apply combination\n press 4 to apply combinations with replacement\n"
val=input(choice)
num=int(val)
for choice in val:
         if (num==1):
           print(p)
         elif (num==2):
            print(q)
         elif (num==3):
            print(c)
         elif (num==4):
            print(d)
         else:
            print("error")

No comments:

Post a Comment