Hướng dẫn permutations python without itertools
Itertools in Python is a module that produces complex iterators with the help of methods that work on iterators. This module works as a fast, memory-efficient tool that is used either by itself or in combination to form iterator algebra. Show Printing Combinations Using itertoolsUsing Itertools we can display all the possible combinations of the string in a quite optimized way. To display the combination requires 2 parameters. First is the string and the second is the length of substrings needed. The following example makes all combinations for the string ‘abc’ using itertools. Example: Python3
Output [('a', 'b'), ('a', 'c'), ('b', 'c')] Printing Combinations Without using itertoolsA. Using recursionTo create combinations without using itertools, iterate the list one by one and fix the first element of the list and make combinations with the remaining list. Similarly, iterate with all the list elements one by one by recursion of the remaining list. Python3
Output [['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b']] B. By using iterationsIn this, return the first combination of n elements from the string as it is, then other combinations are made by considering each element by its position. Each element is treated as unique based on its position, not on its value. So if the input elements are unique, there will be no repeat values in each combination. Python3
Output: [('a', 'b'), ('a', 'c'), ('b', 'c')] |