https://github.com/tylerburdsall/lazy-cartesian-product-python
Find the Nth entry in a Cartesian Product
https://github.com/tylerburdsall/lazy-cartesian-product-python
cartesian cartesian-product combination generate nth python
Last synced: 4 months ago
JSON representation
Find the Nth entry in a Cartesian Product
- Host: GitHub
- URL: https://github.com/tylerburdsall/lazy-cartesian-product-python
- Owner: tylerburdsall
- License: bsd-2-clause
- Created: 2018-03-20T20:47:17.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-18T01:30:25.000Z (over 7 years ago)
- Last Synced: 2025-06-09T21:05:04.863Z (7 months ago)
- Topics: cartesian, cartesian-product, combination, generate, nth, python
- Language: Python
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lazy-cartesian-product
Find the Nth entry in a Cartesian Product
## Usage
Given a list of lists, you can find the Nth entry in a Cartesian Product without precomputing every combination. This saves memory and time while also maintaning close to O(1) performance:
```
from LazyCartesianProduct import LazyCartesianProduct
def example():
a = [ 1, 2, 3, 4, 5 ]
b = [ 'foo', 'bar' ]
c = [ 'x', 'y', 'z' ]
d = [ 'one', 'two', 'three' ]
sets = [ a, b, c, d ]
cp = LazyCartesianProduct(sets)
result1 = cp.entryAt(8)
print(result1)
result2 = cp.entryAt(32)
print(result2)
result3 = cp.entryAt(67)
print(result3)
example()
```
When ran, the following output is produced:
```
$ python example.py
[1, 'foo', 'z', 'three']
[2, 'bar', 'y', 'three']
[4, 'bar', 'y', 'two']
$
```
## License
BSD-2, see LICENSE