Python combo that does not make use of itertools
There are various situations in which we are tasked with extracting unique combinations from individual strings or from distinct collections of integers.
Python provides us with the itertools module, which is the most widely used tool for locating various combinations and permutations. Using this module, we may locate such combinations.
This component is a very useful tool that can find all of the available combinations in a very short amount of time. But finding the combinations does not need us to rely just on the itertools module functions; there are other viable methods.
In this lesson, we will learn about the various ways in which we can find distinct combinations from a string in Python without utilizing any of the built-in string functions.
itertools.
Combinations in Python without using itertools
In this part of the tutorial, we will use Python to construct programs that will find combinations by incorporating many methods into those programs. Within our Python program, we will be utilizing the following techniques.
program:
- Using Iteration method
- Using recursion method
In both approaches, we will begin by analyzing the program to determine how it functions, and only after that will we proceed to the explanation section to learn about the implementation that is being used.
it.
Python combinations using iteration method:
Importing the numpy library so that we may make use of its functions is required in order to put the iterative technique into the program. Let’s get a grasp on the following, shall we?
example.
Example:
-
# Import numpy module in program
-
import numpy as np
-
# Default function to use iterative approach
-
def RecurCombo(iterArray, num):
-
char
=
tuple
(iterArray) # converting input string into a tuple
-
m
=
len
(char) # length of string or tuple
-
if num
>
m: # checking if length of combinations is more than length of string
-
return
-
index
=
np
.arange(num) # using numpy arrange() function
-
# Yielding the first sequence
-
yield tuple(char[i] for i in index)
-
# Using while loop with true condition
-
while True:
-
# iterating over the tuple we made using reversed for loop
-
for a in reversed(range(num)):
-
if index[a] != a + m – num:
-
break
-
else:
-
return
-
index[a] += 1
-
# another for loop iteration
-
for b in range(a + 1, num):
-
index[b] = index[b – 1] + 1
-
yield tuple(char[a] for a in index) # yielding possible combinations from given string
-
# Taking an input string from user
-
input
inputArray
= input(“Enter an input string to find combinations: “)
-
# Printing different combinations as result in output
-
print(“All possible combinations of three letter sets from the string given by you is: “)
-
print([x for x in RecurCombo(inputArray, 3)])
Output:
Explanation for
Enter an input string to find combinations: JavaTpoint All possible combinations of three letter sets from the string given by you is: [('J', 'a', 'v'), ('J', 'a', 'a'), ('J', 'a', 'T'), ('J', 'a', 'p'), ('J', 'a', 'o'), ('J', 'a', 'i'), ('J', 'a', 'n'), ('J', 'a', 't')]
:
In the program that you just saw, we found combinations by iterating through the input string using the process known as iteration.
First, we put the input string and the length of the combinational set into a default Python function and passed in those values as parameters. After that, we turned the string that was provided as input into a tuple. In addition to this, we tested to make sure that the minimum length of the combination did not exceed the maximum length of the string.
Following that, we made use of the arrange() method that is available in numpy in order to establish the index for the tuple. We are going to perform an iteration with the index variable across the tuple.
After that, we iterated over the tuple by utilizing a reverse for loop as well as another for loop inside of the while loop. Following the iteration in loops, we now have a list of all of the potential combinations that satisfy the prerequisite length.
The user then provided us with a string of input, which we have now used. In the final step, we gave back the combinations of all three sets that were given.
string.
Python combinations using recursion method:
With the Recursive method technique, we will be going through the process of iterating over the list by that is made up of a list of strings. Let’s get a grasp on the following, shall we?
example.
Example:
-
# Default Python function to use recursive approach
-
def RecurCombo(array, num):
-
if
num
== 0:
-
return [[]] # if length for combination is 0
-
l
=[] # list to printed in result
-
# Using for loop to implement recursive approach
-
for j in range(0, len(array)):
-
emptyArray
=
array
[j] # define an empty array to print list of sets
-
recurList
=
array
[j + 1:]
-
# Recursion method on list defined in function
-
for x in RecurCombo(recurList, num-1):
-
l.append([emptyArray]+x) # appending list
-
return l # list as result of recursion
-
if
__name__
==”__main__”:
-
# Taking an input string from user
-
input
inputArray
= input(“Enter an input string to find combinations: “)
-
# Printing different combinations as result in output
-
print(“All possible combinations of three letter sets from the string given by you is: “)
-
print(RecurCombo([a for a in inputArray], 3))
Output:
Explanation for
Enter an input string to find combinations: Python All possible combinations of three letter sets from the string given by you is: [['P', 'y', 't'], ['P', 'y', 'h'], ['P', 'y', 'o'], ['P', 'y', 'n'], ['P', 't', 'h'], ['P', 't', 'o'], ['P', 't', 'n'], ['P', 'h', 'o'], ['P', 'h', 'n'], ['P', 'o', 'n'], ['y', 't', 'h'], ['y', 't', 'o'], ['y', 't', 'n'], ['y', 'h', 'o'], ['y', 'h', 'n'], ['y', 'o', 'n'], ['t', 'h', 'o'], ['t', 'h', 'n'], ['t', 'o', 'n'], ['h', 'o', 'n']]
In the program that was just described, we did not make use of any particular Python module when putting the recursive technique into action. In order to implement the recursion method in code, we made use of a default function, just as we did with the iteration method.
Within this program, we made use of a condition in order to check if the combination was of the needed length. After that, we implemented the recursion strategy while still inside the function’s for loop. Following the use of the recursion method, we retrieved combinations from the input string that were of the necessary length. In the end, we asked the user for a string as input and then we returned combinations of three different sets as response.