Advertisement
Guest User

Untitled

a guest
Sep 30th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #get the nth product of `lists`
  2. def product_by_idx(lists, n):
  3. result = []
  4. for seq in reversed(lists):
  5. n, x = divmod(n, len(seq))
  6. result.append(seq[x])
  7. return result[::-1]
  8.  
  9. #find n such that product(lists, n) == target
  10. def idx_from_product(lists, target):
  11. n = 0
  12. for x, seq in zip(target, lists):
  13. n *= len(seq)
  14. n += seq.index(x)
  15. return n
  16.  
  17. def product(lists, from_, to_):
  18. start = idx_from_product(lists, from_)
  19. end = idx_from_product(lists, to_)
  20. for n in range(start, 1+end):
  21. yield product_by_idx(lists, n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement