We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
itertools.combinations()
itertools.combinations()
Sort by
recency
|
894 Discussions
|
Please Login in order to post a comment
Might be lengthy but easy to understand for beginners. Hope this helps:
from itertools import combinations S,k=input().split() S="".join(sorted(S)) k=int(k) for i in range(1,k+1): li=list(combinations(S,i)) li=["".join(i) for i in li] print(*li,sep="\n")
Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations if name == "main": s , k = input().split() final_list=[] for i in range(1,int(k)+1): final=[] result = list(combinations(s,i)) for x in result: x = list(x) x.sort() final.append("".join(x)) final.sort() final_list.extend(final)
print("\n".join(final_list))
Solution without nested for loops (better time complexity)