Forked from tylerburdsall/lazy-cartesian-product.py
Last active
June 15, 2020 01:11
-
-
Save woudsma/654f822037cc65da4fc7e5b8dc912007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from bigfloat import * | |
class LazyCartesianProduct: | |
def __init__(self, sets, context): | |
self.sets = sets | |
self.context = context | |
self.divs = [] | |
self.mods = [] | |
self.maxSize = BigFloat.exact(1) | |
self.precompute() | |
def precompute(self): | |
for i in self.sets: | |
self.maxSize = mul(self.maxSize, BigFloat.exact(len(i)), context=self.context) | |
factor = BigFloat.exact(1) | |
for i in range((len(self.sets) - 1), -1, -1): | |
items = BigFloat.exact(str(len(self.sets[i])), precision=self.context.precision) | |
self.divs.insert(0, factor) | |
self.mods.insert(0, items) | |
factor = mul(factor, items, context=self.context) | |
def entryAt(self, n): | |
if less(n, BigFloat.exact(0)) or greaterequal(n, self.maxSize): | |
raise IndexError | |
combination = [] | |
for i in range(0, len(self.sets)): | |
d = div(n, self.divs[i], context=self.context) | |
m = mod(floor(d, context=self.context), self.mods[i], context=self.context) | |
combination.append(int(m)) | |
return combination |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to automate the steps, part of a cloud-init script I made.