COMBINATORICS


This webpage aims to explore some Python tools for combinatorics. We will use the itertools library for its functions to generate lists of permutations and combinations, as well as the math library for factorial calculations. The Python set object is similar to its mathematical counterpart as it is an unordered collection with no duplicate elements.

A correspondence can be established between certain Python objects and common concepts in set theory:

  • Set : unordered, no duplicates ↔ python set, frozenset
  • Multiset : unordered , allows duplicates ↔ python counter
  • Sequence : ordered, allows duplicates ↔ python list, string, tuple

  • import itertools
    import math
    def permutations_full(word:str):
        for permutation in itertools.permutations(word):
            word=''.join(permutation)
            words.append(word)    
    return (words)
    def anagrams(word:str):
        words = set()
        for permutation in itertools.permutations(word):
            word=''.join(permutation)
            words.add(word)    
        return (words)
    def anagrams_count(word:str):
        count=math.factorial(len(word))
        counted_letters = set()
        for letter in word:
            if letter not in counted_letters:
                count = count / math.factorial(word.count((letter)) )
                counted_letters.add(letter)
            else  :
                pass
        count = int(count)
        return count
    def combinaisons_without_replacement(word:str,n:int):
        return list(itertools.combinations(word,n))
    def permutations_partial(word:str,n:int):
        return list(itertools.permutations(word,2))
    def combinaisons_w_replacement(word:str,n:int):
        return list(itertools.combinations_with_replacement(word,2))

    Anagrams :

    Anagrams
    Anagrams

    Pascal's triangle :

    Pascal Triangle