#!/usr/bin/env python # import itertools; from itertools import permutations two_correct = 0 for itr in permutations(range(4)) : # # Count the number of permutations that have two correct locations: # if itr[0]==0 and itr[1]==1 and ( itr[2]!=2 and itr[3]!=3 ) : two_correct += 1 print "matches 0, 1", itr if itr[0]==0 and itr[2]==2 and ( itr[1]!=1 and itr[3]!=3 ) : two_correct += 1 print "matches 0, 2", itr if itr[0]==0 and itr[3]==3 and ( itr[1]!=1 and itr[2]!=2 ) : two_correct += 1 print "matches 0, 3", itr if itr[1]==1 and itr[2]==2 and ( itr[0]!=0 and itr[3]!=3 ) : two_correct += 1 print "matches 1, 2", itr if itr[1]==1 and itr[3]==3 and ( itr[0]!=0 and itr[2]!=2 ) : two_correct += 1 print "matches 1, 3", itr if itr[2]==2 and itr[3]==3 and ( itr[0]!=0 and itr[1]!=1 ) : two_correct += 1 print "matches 2, 3", itr print "Part (iii): Number of permutations with two correct locations= ", two_correct one_correct = 0 for itr in permutations(range(4)) : # # Count the number of permutations that have one correct location: # if itr[0]==0 and ( itr[1]!=1 and itr[2]!=2 and itr[3]!=3 ) : one_correct += 1 print "matches 0", itr if itr[1]==1 and ( itr[0]!=0 and itr[2]!=2 and itr[3]!=3 ) : one_correct += 1 print "matches 1", itr if itr[2]==2 and ( itr[0]!=0 and itr[1]!=1 and itr[3]!=3 ) : one_correct += 1 print "matches 2", itr if itr[3]==3 and ( itr[0]!=0 and itr[1]!=1 and itr[2]!=2 ) : one_correct += 1 print "matches 3", itr print "Part (iv): Number of permutations with one correct location= ", one_correct