Convert-Group / pairing-functions

A collection of pairing functions.
https://pypi.org/project/pairing-functions
MIT License
30 stars 4 forks source link
cantor mathematical-functions pairing-functions pip szudzik

pairing-functions

A pairing function is a function that reversibly maps two non-negative integers onto a single non-negative integer.

This package currently supports the following pairing functions:

Install

Simply:

$ pip install pairing-functions

Usage

from pairing_functions import cantor, szudzik

szudzik.pair(3, 4)  
// 19

szudzik.unpair(19)
// (3, 4)

cantor.pair(3, 4)  
// 32

cantor.unpair(32)
// (3, 4)

You can also work with more than 2 integers:

from pairing_functions import cantor, szudzik

szudzik.pair(1, 2, 3, 4)
// 1126

cantor.pair(1, 2, 3, 4)
// 2705

# by default, unpairing will result in two integers

szudzik.unpair(1126)
// (33, 4)

cantor.unpair(2705)
// (69, 4)

# but going back to the initial integers is also possible
# just specify how many integers you expect

szudzik.unpair(1126, n=4)
// (1, 2, 3, 4)

cantor.unpair(2705, n=4)
// (1, 2, 3, 4)

Documentation

You can find more about pairing functions in the docs.

How to contribute

If you wish to contribute, you can start from here !

Test

You can run the available tests with pytest - code coverage metrics are also available via pytest-cov.