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 0Method 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 1Method 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)This is a Python Program to accept three distinct digits and print all possible combinations from the digits. Problem Description The program takes three distinct numbers and prints all possible combinations from the digits. Problem Solution 1. Take in the first, second and third number and store it in separate variables. Program/Source Code Here is the source code of the Python Program to accept three distinct digits and prints all possible combinations from the digits. The program output is also shown below.
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k]) Program Explanation 1. User must enter the first, second and third number.
2. Then append
all the three numbers to the list.
3. Use three for loops and print the digits in the list if none of their indexes are equal to each other.
4. Exit.
2. All the elements are
appending into a list for the ease of comparison.
3. The for loops range from 0-2 which are basically the indexes of the three elements in the list.
4. If none of the indexes are equal to each other, the element associated with the particular element in the list is printed.
Runtime Test Cases
Case 1: Enter first number:1 Enter second number:2 Enter third number:3 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Case 2: Enter first number:5 Enter second number:7 Enter third number:3 5 7 3 5 3 7 7 5 3 7 3 5 3 5 7 3 7 5
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.