Wiki

Clone wiki

python_trotter / Home

Home | Installation | Seeking Arrangements | Huge Numbers | About

Trotter

Welcome to trotter, a Python 3 module for representing arrangements commonly encountered in combinatorics.

Trotter classes

Classes have been defined according to whether order is important, items may be repeated, and length is specified.

Class Order Important Repetition Allowed Specified Length
Amalgams Yes Yes Yes
Permutations Yes No Yes
Compounds Yes No No
Compositions No Yes Yes
Combinations No No Yes
Subsets No No No

Instances of these classes are indexable pseudo-lists containing all possible arrangements. Since the number of possible arrangements can grow very quickly with the number of items available (and the number of items taken at a time, where applicable), instances do not actually store all arrangements but are rather containers of mappings between integers and arrangements. This makes it possible to create instances that "contain" very large numbers of arrangements.

Example session: pick three words

# Import the Combinations class.
from trotter import *

# A list of words.
items = ["the", "parrot", "is", "not", "pining"]

# A representation of 3-combinations of these words.
combos = Combinations(3, items)

# Exactly what is c?
print(combos)

A pseudo-list containing 10 3-combinations of ['the', 'parrot', 'is', 'not', 'pining'].
# How many 3-combinations are there, again?
print(len(combos))
10
# Let's see them!
for combo in combos: 
    print(combo)
['the', 'parrot', 'is']
['the', 'parrot', 'not']
['the', 'parrot', 'pining']
['the', 'is', 'not']
['the', 'is', 'pining']
['the', 'not', 'pining']
['parrot', 'is', 'not']
['parrot', 'is', 'pining']
['parrot', 'not', 'pining']
['is', 'not', 'pining']

Example session: subsets of letters in a string

# The items can also be the characters in a string.
items = "spam"
# The subsets of the letters in this word
# (notice the first is the empty string):
for subset in Subsets(items):
    print(subset)
s
p
sp
a
sa
pa
spa
m
sm
pm
spm
am
sam
pam
spam

Example session: a looooong pseudo-list!

# How many 10-permutations are there 
# of the 26 letters in the alphabet?
letters = "abcdefghijklmnopqrstuvwxyz"
permutations = Permutations(10, letters)

# Just how big is this list?
print(permutations)
A pseudo-list containing 19275223968000 10-permutations of abcdefghijklmnopqrstuvwxyz.
Wow! Almost twenty trillion! Luckily it's only a pseudo-list and not really stored on the computer!

# The word "algorithms" is a ten-letter permutation
# of letters. What is the index of this word in the
# list of permutations?
permutations.index("algorithms")
6831894769563
Found in a split second - take that, Mathematica!

# Let's check:
print(permutations[6831894769563])
algorithms

Updated