Sample data:
21 7
Expected output:
51200
I wrote two programs that solve this problem. The first uses the math module permutations to find all possible permutations and count them. You can have a look at the code below. However, this program is inefficient which is why I chose to write a second, faster program.
Slow approach:
import itertools
n = 21
k = 7
count = 0
for p in itertools.permutations(range(1, n + 1), k):
count += 1
print(count % 1000000)
Faster approach:
n = 21
k = 7
partial_perm = 1
for i in range(k):partial_perm *= (n - i)
print(partial_perm % 1000000)
Both of the programs work and complete the problem within the time limits of Rosalind, but the second program is much faster than the first one.
No comments:
Post a Comment