You are to write a program which takes in a pair of positive integers, n and m ,
ID: 3537256 • Letter: Y
Question
You are to write a program which takes in a pair of positive integers, n and m,
(0 < m _ n < 20), and outputs all the possible permutations of choosing m integers from the set f {1, 2, %u2026 , n}. The input is just one line with two integers separated by a white space. The output should print one permutation per line, and a final line containing the number of permutations printed.
Important:
Your program should prompt the user for input, and it should accept only valid input. Your output should be organized as follows:
Sample Input Sample Output
3 2 1 2
1 3
2 1
2 3
3 1
3 2
6
Explanation / Answer
#!/usr/bin/env python
def permutate(l, n, nl):
global count
if n == 0:
if len(nl) != len(set(nl)): return
nl1 = []
for i in nl: nl1.append(l[i])
print " ".join([str(x) for x in nl1])
count=count+1
else:
n = n - 1
nl1 = [x for x in nl]
for i in xrange(0, len(l)):
nl = [x for x in nl1]
nl.append(i)
permutate(l, n, nl)
del nl[:]
def permutations(l,m):
permutate(l,m , [])
n,m=raw_input("Enter integers n and m ").split()
count =0
permutations(range(1,int(n)+1),int(m))
print count
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.