You are to write a program which takes in a pair of positive integers, n and m,
ID: 3557797 • 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
def perm(nlist, m):
if m == 0:
yield []
return
for list_i in nlist:
temp = list(nlist)
temp.remove(list_i)
for p in perm(temp, m-1):
yield [list_i] + p
a=raw_input()
b = a.split(' ')
n = int (b[0])
m = int (b[1])
alist = []
for i in range( 1, n+1 ):
alist.append(i)
cc = perm( alist, m)
count = 0
for i in cc:
count = count + 1
print i
print count
Well indented code with input output:
http://ideone.com/Y2vfKa
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.