Header Ad

HackerRank itertools.permutations() solution in Python

In this HackerRank itertools.permutations() problem solution in python This tool returns successive r length permutations of elements in an iterable.

If r is not specified or is None, then r defaults to the length of the iterable, and all possible full length permutations are generated.

Permutations are printed in a lexicographic sorted order. So, if the input iterable is sorted, the permutation tuples will be produced in a sorted order.

You are given a string S.

Your task is to print all possible permutations of size k of the string in lexicographic sorted order.

HackerRank itertools.permutations() solution in Python

Problem solution in Python 2 programming.

from itertools import permutations

S,k = raw_input().split()
k = int(k)

print '\n'.join(sorted(''.join(p) for p in permutations(S,k)))


Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import permutations
s,n = input().split()
print(*[''.join(i) for i in permutations(sorted(s),int(n))],sep='\n')


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import permutations
string_number = raw_input().split()
string = string_number[0]
number = map(int, string_number[1])


for elem in sorted(list(permutations(string, number[0]))):
    n = len(elem)
    elem_print = ""
    for i in range(0, n):     
        elem_print+=elem[i]
    print elem_print


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import itertools

ll, n = input().split()

for i in itertools.permutations(sorted(ll),int(n)):
    print("".join(i))


Post a Comment

0 Comments