You can use the builtin module itertools
in python. Refer to this question already asked here
import itertools
array = itertools.permutations[[1, 2, 3, 4]]
for eachpermutation in array:
print[eachpermutation ]
Should give you the output such as this
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 1, 3]
[2, 4, 3, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 1, 2, 3]
[4, 1, 3, 2]
[4, 2, 1, 3]
[4, 2, 3, 1]
[4, 3, 1, 2]
[4, 3, 2, 1]
If you need to concatenate the sublists into a single number, you can use the answer provided here
for eachpermutation in array:
print[int[''.join[str[i] for i in eachpermutation ]]]
gives you the following output
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Hope that helps
Given 3 digits a, b, and c. The task is to find all the possible combinations from these digits.
Examples:
Input: [1, 2, 3] Output: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Input: [0, 9, 5] Output: 0 9 5 0 5 9 9 0 5 9 5 0 5 0 9 5 9 0
Method 1: Brute force or Naive approach
The naive approach is to run 3 loops from 0 to 3 and print all the numbers from the list if the indexes are not equal to each other.
Example:
Python3
def
comb[L]:
for
i
in
range
[
3
]:
for
j
in
range
[
3
]:
for
k
in
range
[
3
]:
if
[i!
=
j
and
j!
=
k
and
i!
=
k]:
print
[L[i], L[j], L[k]]
comb[[
1
,
2
,
3
]]
Output:
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Method 2: Using itertools.permutations[]
This method takes a list as an input and returns an object list of tuples that contain all permutation in a list form.
Example:
Python3
from
itertools
import
permutations
comb
=
permutations[[
1
,
2
,
3
],
3
]
for
i
in
comb:
print
[i]
Output:
[1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
Way too complicated solution for something a basic loop would do. Creates unnecessary dependencies, might be slower?
Edit: Time difference is negligible [0.5%, roughly 8.5/100 seconds when test-running it 10000 times]
Difference is also that your approach creates leading zeroes, which [as far as I understood] were unnecessary.
Also, you have to pass a list of every digit and allow 4 repetitions, not simply passing 4 digits.
While learning to code it’s easy to push through from exercise to exercise,
chasing the feeling of accomplishment. I find that everything sinks in more, though, if I take a moment to flesh out my thought process from time to time. I came across a problem on Hackerrank the other day that required me to find all possible sums of four out of five integers in a list. I’m making a note of what I did here, for future reference. I used the
combinations attribute from the itertools module. Using this as our list: Then passing it into our function: The result is: The purpose of the exercise was to find the smallest and largest sum of these possible combinations. To
do this, I fleshed out my code as follows:Mini-learns with Python 3
Photo by Chandan Siddaramaiah on UnsplashTo import this into our Python REPL or code
from itertools import combinations
The function
def find_combos[arr]:
combos = list[combinations[arr, 4]]
print[combos]Breaking this down
arr
— This will be the list we pass into the find_combos
function.
Ex: [1, 2, 3, 4, 5]
combos = list[combinations[arr, 4]]
— Combinations takes two parameters. The first [arr
] is what we are iterating over. The second [4
], is how many of those things in arr
[e.g — our list of numbers, strings, whatever] that we want to combine. All of the combinations are turned into a list using list
, then set to a variable named combos
.combinations
— This is what we imported from itertools that does the dirty work behind the scenes. The code for this can
be found in the Python documentation: //docs.python.org/3/library/itertools.html#itertools.combinationslist
— This is a built-in function in Python. My layperson’s way of understanding this is that it turns all those combinations into a list that we can iterate over:
//docs.python.org/3/library/stdtypes.html#listsThe output
arr = [1, 2, 3, 4, 5]
find_combos[arr]
[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
Taking it further
def find_combos[arr]:
combos = list[combinations[arr, 4]]
combo_sums = []
for combo in combos:
combo_sums.append[sum[combo]]
print[min[combo_sums], max[combo_sums]]combo_sums = []
— This creates an empty list to dump all my sums into.combo
[or combination of four numbers that we saw in the above result: [[1, 2, 3, 4], [1, 2, 3, 5]…
etc.], takes their sum
, and appends the sum to the combo_sums
list. The end result of the combo_sums
list is:[10, 11, 12, 13, 14]
print[min[combo_sums], max[combo_sums]]
— This prints the minimum number in combo_sums
and the maximum number:10 14