Giter Club home page Giter Club logo

python-trotter's Introduction

Trotter

Welcome to trotter, a set of Python classes for representing sequences of structures of item selections commonly encountered in combinatorics.

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 selections of items. 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.

Installation

pip install git+https://github.com/ram6ler/python-trotter

Example: combinations of words

from trotter import Combinations

items = ["the", "parrot", "is", "not", "pining"]
combos = Combinations(3, items)

print(repr(combos))
Combinations(3, ['the', 'parrot', 'is', 'not', 'pining'])
print(str(combos))
A pseudo-list containing 10 3-combinations of ['the', 'parrot', 'is', 'not', 'pining'].
print(len(combos))
10
for combo in combos:
    print(" ".join(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
print(combos.index("the parrot pining".split()))
2
print(combos[2])
['the', 'parrot', 'pining']

Example: subsets of characters in a string

The items can be presented as a list of objects or a string, which is interpreted as a list of characters. Here's an example where we use a string.

for i, subset in enumerate(Subsets("spam")):
     print(f"[{i}] '{subset}'")
[0] ''
[1] 's'
[2] 'p'
[3] 'sp'
[4] 'a'
[5] 'sa'
[6] 'pa'
[7] 'spa'
[8] 'm'
[9] 'sm'
[10] 'pm'
[11] 'spm'
[12] 'am'
[13] 'sam'
[14] 'pam'
[15] 'spam'

Example: many permutations!

from trotter import Permutations
letters = "abcdefghijklmnopqrstuvwxyz"
permutations = Permutations(10, letters)
print(permutations)
A pseudo-list containing 19275223968000 10-permutations of 'abcdefghijklmnopqrstuvwxyz'.

That's almost twenty trillion! Luckily, we're only dealing with a pseudo-list, and those permutations are not actually stored!

Notice that the word algorithms is a ten-letter permutation of the letters of the alphabet. At what position in the pseudo-list is this word?

print(permutations.index("algorithms"))
6831894769563

Luckily, we were able to find it without a brute-force search! Let's check that result...

print(permutations[6831894769563])
algorithms

python-trotter's People

Contributors

ram6ler avatar

Stargazers

 avatar

Watchers

 avatar  avatar

python-trotter's Issues

Overflows checking len() of large pseudo-lists

import Trotter
import secrets
import sys

base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
my_amalgams = trotter.Amalgams(11, base58)
print(my_amalgams)
A pseudo-list containing 24986644000165537792 11-amalgams of '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.

But if I try to use the very long my_amalgams

random_pick = secrets.choice(my_amalgams)
OverflowError: cannot fit 'int' into an index-sized integer

Same thing directly using len()

list_length = len(my_amalgams)
OverflowError: cannot fit 'int' into an index-sized integer

I'm using Python 3.10 and sys.maxsize is 9223372036854775807

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.