Last active
September 27, 2018 13:08
-
-
Save onlyforbopi/1076296b6aae794c3f1f4723568a1e7a to your computer and use it in GitHub Desktop.
Python.Math.Permutations.Combinations #python #Python #permutations #combinations #itertools #PythonModules PERMUTATIONS AND COMBINATIONS - SORT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Fri Mar 31 09:49:49 2017 | |
| @author: P.Doulgeridis | |
| """ | |
| import itertools | |
| def permutations(iterable, r=None): | |
| # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC | |
| # permutations(range(3)) --> 012 021 102 120 201 210 | |
| pool = tuple(iterable) | |
| n = len(pool) | |
| r = n if r is None else r | |
| if r > n: | |
| print("Combination length required longer than populace") | |
| return | |
| indices = list(range(n)) | |
| cycles = list(range(n, n-r, -1)) | |
| yield tuple(pool[i] for i in indices[:r]) | |
| while n: | |
| for i in reversed(range(r)): | |
| cycles[i] -= 1 | |
| if cycles[i] == 0: | |
| indices[i:] = indices[i+1:] + indices[i:i+1] | |
| cycles[i] = n - i | |
| else: | |
| j = cycles[i] | |
| indices[i], indices[-j] = indices[-j], indices[i] | |
| yield tuple(pool[i] for i in indices[:r]) | |
| break | |
| else: | |
| return | |
| def permutations2(iterable, r=None): | |
| pool = tuple(iterable) | |
| n = len(pool) | |
| r = n if r is None else r | |
| for indices in product(range(n), repeat=r): | |
| if len(set(indices)) == r: | |
| yield tuple(pool[i] for i in indices) | |
| a = [ 1, 2, 3, 7, "the" ] | |
| c = [ 5, 6, 8, 9, "ifthen" ] | |
| b = '4567' | |
| # print(list(permutations(b, 3))) | |
| # print(list(permutations(a, 5))) | |
| # print(list(permutations(a, 4))) | |
| # Permutation (Order matters) | |
| #print (list(itertools.permutations(a, 3))) | |
| # Combination (Order does not matter) | |
| #print (list(itertools.combinations(a, 3))) | |
| # Cartesian product (with several iterables - combine each with each one by one) | |
| #print (list(itertools.product(a, c))) | |
| # Cartesian product (with one iterable by itself, repeat factor) | |
| #print (list(itertools.product(a, repeat=4))) | |
| def permutation_generate(input, slots=2): | |
| try: | |
| import itertools | |
| except ImportError: | |
| return([],"ImportError: Itertools") | |
| output_list = [] | |
| count = 0 | |
| calc = list(itertools.permutations(input, slots)) | |
| count = len(calc) | |
| return [count, calc] | |
| def combination_generate(input, slots=2): | |
| try: | |
| import itertools | |
| except ImportError: | |
| return([],"ImportError: Itertools") | |
| output_list = [] | |
| count = 0 | |
| calc = list(itertools.combinations(input, slots)) | |
| count = len(calc) | |
| return [count, calc] | |
| def combi_repl_generate(input, slots=2): | |
| checks = True | |
| try: | |
| import itertools | |
| except ImportError: | |
| return([],"ImportError: Itertools") | |
| output_list = [] | |
| count = 0 | |
| calc = list(itertools.combinations_with_replacement(input, slots)) | |
| count = len(calc) | |
| return [count, calc] | |
| def cartesian_prod_generate(input1, input2): | |
| try: | |
| import itertools | |
| except ImportError: | |
| return([],"ImportError: Itertools") | |
| output_list = [] | |
| count = 0 | |
| calc = list(itertools.product(a, c)) | |
| count = len(calc) | |
| return [count, calc] | |
| def cartesian_prod_repeat(input1, integer): | |
| try: | |
| import itertools | |
| except ImportError: | |
| return([],"ImportError: Itertools") | |
| output_list = [] | |
| count = 0 | |
| calc = list(itertools.product(a, repeat=integer)) | |
| count = len(calc) | |
| return [count, calc] | |
| #print (permutation_generate(a, 4)) | |
| # print (combination_generate(a, 3)) | |
| # print (combi_repl_generate(a, 3)) | |
| #print (cartesian_prod_generate(a, c)) | |
| #print (cartesian_prod_repeat(c, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment