Write a program to accept numbers in an array and from that numbers you have to print all possible combinations ex: if there are 5 numbers as 42163 then output should be like 12346, 12364,12436 and so on. So how to solve this?
12/9/2020 5:46:49 PM
Achintya Raj9 Answers
New AnswerFor this kind of task you can use python module "itertools", then you can use permutations (as your description is showing) from itertools import permutations lst = [1,2,3,4,6] ... # your code follows here. ...
You want to print all distinct permutations of some number or string. You can see some algorithm for it here: https://www.geeksforgeeks.org/distinct-permutations-string-set-2/ I generally use next_permutation() of C++ stl, sort the string so that you can get lexicographically smallest string and then just keep finding next_permutation() until it returns false(means no next lexicographically greater permutation possible) //see method2 here https://www.geeksforgeeks.org/permutations-of-a-given-string-using-stl/
Sorry I want all the combinations I didn't read carefully that time Let me tell u with a small ex. Suppose there are three numbers 2 4 1 then possible combinations for this are : 241,214,412,421,124,142 i.e 6 combinations
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message