Advertisement
Guest User

Untitled

a guest
Sep 30th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import itertools
  2.  
  3. def mixedBaseCounter(radixes, state=None):
  4. if state is None:
  5. state = [0] * len(radixes)
  6. while True:
  7. yield state
  8. state[-1] += 1
  9. for idx in range(len(radixes)-1, 0, -1):
  10. if state[idx] == radixes[idx]:
  11. state[idx] = 0
  12. state[idx-1] += 1
  13. else:
  14. break
  15. if state[0] == radixes[0]:
  16. return
  17.  
  18. def pm_product(lists, from_, to_):
  19. starting_indices = [lists[i].index(x) for i,x in enumerate(from_)]
  20. ending_indices = [lists[i].index(x) for i,x in enumerate(to_)]
  21. radixes = [len(seq) for seq in lists]
  22. for indices in mixedBaseCounter(radixes, starting_indices):
  23. result = [lists[i][idx] for i, idx in enumerate(indices)]
  24. yield result
  25. if indices == ending_indices:
  26. break
  27.  
  28. def miyagi_product(somelists, first, last):
  29. first_index, radix = 0, 1
  30. for sub_list, sub_first in zip(reversed(somelists), reversed(first)):
  31. first_index += sub_list.index(sub_first) * radix
  32. radix *= len(sub_list)
  33.  
  34. for element in itertools.islice(itertools.product(*somelists), first_index, None):
  35. yield element
  36. if x == last:
  37. break
  38.  
  39. #get the nth product of `lists`
  40. def product_by_idx(lists, n):
  41. result = []
  42. for seq in reversed(lists):
  43. n, x = divmod(n, len(seq))
  44. result.append(seq[x])
  45. return result[::-1]
  46.  
  47. #find n such that product(lists, n) == target
  48. def idx_from_product(lists, target):
  49. n = 0
  50. for x, seq in zip(target, lists):
  51. n *= len(seq)
  52. n += seq.index(x)
  53. return n
  54.  
  55. def kevin_product(lists, from_, to_):
  56. start = idx_from_product(lists, from_)
  57. end = idx_from_product(lists, to_)
  58. for n in range(start, 1+end):
  59. yield product_by_idx(lists, n)
  60.  
  61. somelists = [
  62. [0,1,2,3,4,5,6,7,8,9],
  63. [0,1,2,3,4,5,6,7,8,9],
  64. [0,1,2,3,4,5,6,7,8,9],
  65. [0,1,2,3,4,5,6,7,8,9],
  66. [0,1,2,3,4,5,6,7,8,9],
  67. [0,1,2,3,4,5,6,7,8,9],
  68. [0,1,2,3,4,5,6,7,8,9],
  69. ]
  70.  
  71. import time
  72. args = (somelists, (9,9,9,0,0,0,0), (9,9,9,9,9,9,9))
  73.  
  74. start = time.time()
  75. for x in miyagi_product(*args):
  76. pass
  77. print(f"Completed in {time.time() - start} seconds")
  78.  
  79. start = time.time()
  80. for x in kevin_product(*args):
  81. pass
  82. print(f"Completed in {time.time() - start} seconds")
  83. start = time.time()
  84.  
  85. for x in pm_product(*args):
  86. pass
  87. print(f"Completed in {time.time() - start} seconds")
  88.  
  89. #results on my machine:
  90. #Completed in 0.38527798652648926 seconds
  91. #Completed in 0.1340954303741455 seconds
  92. #Completed in 0.13209939002990723 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement