How do you find the combinations of 4 numbers in python?

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.

Mini-learns with Python 3

Photo by Chandan Siddaramaiah on Unsplash

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.

To 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.combinations
  • list — 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#lists

The output

Using this as our list:

arr = [1, 2, 3, 4, 5]

Then passing it into our function:

find_combos[arr]

The result is:

[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]

Taking it further

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:

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.
  • The for loop iterates through each 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

How do you get all the combinations of a set of numbers in Python?

To find all the combinations of a Python list, also known as a powerset, follow these steps:.
Import the built-in itertools module..
Specify a list of items..
Initialize an empty list for storing the combinations..
Create a loop that loops values from 0 to the length of the list + 1..

How many combinations are possible with 4 items?

I.e. there are 4 objects, so the total number of possible combinations that they can be arranged in is 4! = 4 x 3 x 2 x 1 = 24.

How many combinations of the numbers 1 2 3 4 are there?

Explanation: If we are looking at the number of numbers we can create using the numbers 1, 2, 3, and 4, we can calculate that the following way: for each digit [thousands, hundreds, tens, ones], we have 4 choices of numbers. And so we can create 4×4×4×4=44=256 numbers.

Chủ Đề