A simple library and tool for generating anagrams. It is generally low memory and feels reasonably fast.
The implementation here is mildly interesting. Rather than descend in a predictable way through all possible anagrams in sequence using an anagram, the generator simply keeps track of every anagram it's seen. Each iteration randomly shuffles the text around. If it encounters a shuffle it's seen before, it just tries again until a new one is found.
A few helpful utils are shipped in the lib
module and available as top
level imports. permutations
is the main utility; it is implemented as a
generator and yields a new anagram of the given string each iteration until all
possible anagrams are exhausted.
from maagnar import permutations
for anagram in permutations("sometext"):
print(anagram)
You can also determine how many possible anagrams exist for a given string:
>>> from maagnar import calculate_total_permutations
>>> calculate_total_permutations("sometext")
10080
>>> calculate_total_permutations("abcd")
24
>>> calculate_total_permutations("abca")
12
Installation includes an entry point called maagnar
which can be use to
generate an anagrams list.
$ maagnar lol
INFO: Generating anagrams from lol
INFO: Possible combinations: 3
INFO: [1/3] Found: 'lol'
INFO: [2/3] Found: 'oll'
INFO: [3/3] Found: 'llo'
$
The entry point remembers previously seen anagrams. This isn't that interesting when the number of possible anagrams is low, but for anagrams such as:
Just because some of us can read and write and do a little math, that doesn't mean we deserve to conquer the universe
The number possible permutations is extremely high. You might need to stop after a few decades and let your computer rest for a while before returning to the task in-progress.
To start an anagram generator over, use the --clear-seen
switch:
$ maagnar lol --clear-seen
INFO: Generating anagrams from lol
INFO: Possible combinations: 3
INFO: [1/3] Found: 'lol'
INFO: [2/3] Found: 'llo'
INFO: [3/3] Found: 'oll'
$
I'm on pypi!
$ pip install maagnar
- Simple to use
- Compatibility with Python 3.x, PyPy
This project uses a Makefile for various tasks. Some of the available tasks are listed below.
make clean
- Clean build artifacts out of your projectmake test
- Run Unit Tests (using pytest & tox)make sdist
- Build a Python source distributionmake wheel
- Build a Python wheelmake artifacts
- Builds both sdist and wheelmake tox
- Run tests withtox
to test against multiple Python versionsmake mypy
- Run the static code analysis toolmake publish
- publish any artifacts in dist/* using twinemake format-code
- Format the code using theblack
formattermake
- Equivalent tomake test sdist wheel
Maagnar honors the NO_COLOR
environment variable.
- Instead of using a set to track seen anagrams, store all seen values in a fanout cache on-disk; this reduces overall memory consumption though increases disk usage. (Issue #1)
- Added
--clear-seen
command line argument to start anagram generation over from scratch.
Add long description for pypi publication
Feature complete