Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- def miyagi_product(somelists, first, last):
- first_index, radix = 0, 1
- for sub_list, sub_first in zip(reversed(somelists), reversed(first)):
- first_index += sub_list.index(sub_first) * radix
- radix *= len(sub_list)
- for element in itertools.islice(itertools.product(*somelists), first_index, None):
- yield element
- if x == last:
- break
- #get the nth product of `lists`
- def product_by_idx(lists, n):
- result = []
- for seq in reversed(lists):
- n, x = divmod(n, len(seq))
- result.append(seq[x])
- return result[::-1]
- #find n such that product(lists, n) == target
- def idx_from_product(lists, target):
- n = 0
- for x, seq in zip(target, lists):
- n *= len(seq)
- n += seq.index(x)
- return n
- def kevin_product(lists, from_, to_):
- start = idx_from_product(lists, from_)
- end = idx_from_product(lists, to_)
- for n in range(start, 1+end):
- yield product_by_idx(lists, n)
- somelists = [
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- [0,1,2,3,4,5,6,7,8,9],
- ]
- import time
- args = (somelists, (9,9,9,9,9,9,0), (9,9,9,9,9,9,9))
- start = time.time()
- for x in miyagi_product(*args):
- pass
- print(f"Completed in {time.time() - start} seconds")
- start = time.time()
- for x in kevin_product(*args):
- pass
- print(f"Completed in {time.time() - start} seconds")
- #results on my machine:
- #Completed in 0.348247766494751 seconds
- #Completed in 0.0009999275207519531 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement