Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Itertools: Combinatoric Iterators

We had learnt about the Infinite Iterators. Now, we will look at the type of Combinatoric Iterators

Combinatoric Iterators

The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products are called combinatoric iterators.

In Python there are 4 combinatoric iterators:

Product(): This tool computes the cartesian product of input iterables. To compute the product of an iterable with itself, we use the optional repeat keyword argument to specify the number of repetitions.
The output of this function are tuples in sorted order.

from itertools import product  
    
print(list(product([1, 2], repeat = 2)))  
# prints [(1, 1), (1, 2), (2, 1), (2, 2)]

print(list(product([1, 2], '2')))  
# prints [(1, '2'), (2, '2')]
print(list(product('AB', [3, 4]))) 
# prints [('A', 3), ('A', 4), ('B', 3), ('B', 4)]

Permutations(): Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes length of the iterable.

from itertools import permutations  
    
print (list(permutations([1, 'InterviewBit'], 2)))
# prints [(1, 'InterviewBit'), ('InterviewBit', 1)]

print (list(permutations('AB')))  
# prints [('A', 'B'), ('B', 'A')]

print(list(permutations(range(3), 2)))
# prints [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

Combinations(): This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.

from itertools import combinations  
    
print(list(combinations(['A', 2], 2)))  
# prints [('A', 2)]

print(list(combinations('AB', 2)))  
# prints [('A', 'B')]

print(list(combinations(range(2), 1)))
# prints [(0,), (1,)]

Combinations_with_replacement(): This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function.

from itertools import combinations_with_replacement  
    
print(list(combinations_with_replacement("AB", 2)))  
# prints [('A', 'A'), ('A', 'B'), ('B', 'B')]

print(list(combinations_with_replacement([1, 2], 2)))  
# prints [(1, 1), (1, 2), (2, 2)]

print(list(combinations_with_replacement(range(2), 1))) 
# prints [(0,), (1,)]

Try the following excercise in the editor below.

You are given a string S containing distinct characters.
Your task is to print all possible permutations of size K of the string in lexicographic sorted order.

Constraints

1 <= K <= len(S)
String S consist of lowercase letters.

Input Format

First line contains space separated input S and K.

Output Format

Print the permutations of the string S of size K on separate lines.

Example Input

one 2

Example Output

eo
en
ne
no
oe
on
Start solving Itertools: Combinatoric Iterators on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test