Back in July of 2010 I blogged about displaying a set of repeating permutations of a number set. In this blog post I explained how my daughter had a kid’s “spy safe” that had four buttons on it that could be used to enter a passcode. At the time I crafted a bit of Java code to display all possible combinations that could be entered to unlock and open the safe.
Now in my quest to explore the Python itertools module I wanted to try this exercise again. Turns out that Python’s itertools module makes this easy with a method called product(). A product is defined as ”…a mathematical operation which returns a set (or product set) from multiple sets”. To do this the product() method takes an argument that is an iterable and a range. Range in this case indicates how many time to repeat the product set. So in our example, since you have to enter in 4 numbers for a passcode we would repeat the product four times.
from itertools import combinations_with_replacement, product
#
# Available numbers are 1 - 4
#
availableNumbers = range(1, 5)
#
# Enumerate over a product of the available number range.
#
for i, s in enumerate(product(availableNumbers, repeat=4)):
print "Combination #%s: %s" % (i, s)
This gives us an output that looks like this:
Combination #0: (1, 1, 1, 1)
Combination #1: (1, 1, 1, 2)
Combination #2: (1, 1, 1, 3)
Combination #3: (1, 1, 1, 4)
Combination #4: (1, 1, 2, 1)
Combination #5: (1, 1, 2, 2)
Combination #6: (1, 1, 2, 3)
Combination #7: (1, 1, 2, 4)
Combination #8: (1, 1, 3, 1)
Combination #9: (1, 1, 3, 2)
Combination #10: (1, 1, 3, 3)
Combination #11: (1, 1, 3, 4)
Combination #12: (1, 1, 4, 1)
Combination #13: (1, 1, 4, 2)
Combination #14: (1, 1, 4, 3)
Combination #15: (1, 1, 4, 4)
Combination #16: (1, 2, 1, 1)
Combination #17: (1, 2, 1, 2)
Combination #18: (1, 2, 1, 3)
Combination #19: (1, 2, 1, 4)
Combination #20: (1, 2, 2, 1)
Combination #21: (1, 2, 2, 2)
Combination #22: (1, 2, 2, 3)
Combination #23: (1, 2, 2, 4)
Combination #24: (1, 2, 3, 1)
Combination #25: (1, 2, 3, 2)
Combination #26: (1, 2, 3, 3)
Combination #27: (1, 2, 3, 4)
Combination #28: (1, 2, 4, 1)
Combination #29: (1, 2, 4, 2)
Combination #30: (1, 2, 4, 3)
Combination #31: (1, 2, 4, 4)
Combination #32: (1, 3, 1, 1)
Combination #33: (1, 3, 1, 2)
Combination #34: (1, 3, 1, 3)
Combination #35: (1, 3, 1, 4)
Combination #36: (1, 3, 2, 1)
Combination #37: (1, 3, 2, 2)
Combination #38: (1, 3, 2, 3)
Combination #39: (1, 3, 2, 4)
Combination #40: (1, 3, 3, 1)
Combination #41: (1, 3, 3, 2)
Combination #42: (1, 3, 3, 3)
Combination #43: (1, 3, 3, 4)
Combination #44: (1, 3, 4, 1)
Combination #45: (1, 3, 4, 2)
Combination #46: (1, 3, 4, 3)
Combination #47: (1, 3, 4, 4)
Combination #48: (1, 4, 1, 1)
Combination #49: (1, 4, 1, 2)
Combination #50: (1, 4, 1, 3)
Combination #51: (1, 4, 1, 4)
Combination #52: (1, 4, 2, 1)
Combination #53: (1, 4, 2, 2)
Combination #54: (1, 4, 2, 3)
Combination #55: (1, 4, 2, 4)
Combination #56: (1, 4, 3, 1)
Combination #57: (1, 4, 3, 2)
Combination #58: (1, 4, 3, 3)
Combination #59: (1, 4, 3, 4)
Combination #60: (1, 4, 4, 1)
Combination #61: (1, 4, 4, 2)
Combination #62: (1, 4, 4, 3)
Combination #63: (1, 4, 4, 4)
Combination #64: (2, 1, 1, 1)
Combination #65: (2, 1, 1, 2)
Combination #66: (2, 1, 1, 3)
Combination #67: (2, 1, 1, 4)
Combination #68: (2, 1, 2, 1)
Combination #69: (2, 1, 2, 2)
Combination #70: (2, 1, 2, 3)
Combination #71: (2, 1, 2, 4)
Combination #72: (2, 1, 3, 1)
Combination #73: (2, 1, 3, 2)
Combination #74: (2, 1, 3, 3)
Combination #75: (2, 1, 3, 4)
Combination #76: (2, 1, 4, 1)
Combination #77: (2, 1, 4, 2)
Combination #78: (2, 1, 4, 3)
Combination #79: (2, 1, 4, 4)
Combination #80: (2, 2, 1, 1)
Combination #81: (2, 2, 1, 2)
Combination #82: (2, 2, 1, 3)
Combination #83: (2, 2, 1, 4)
Combination #84: (2, 2, 2, 1)
Combination #85: (2, 2, 2, 2)
Combination #86: (2, 2, 2, 3)
Combination #87: (2, 2, 2, 4)
Combination #88: (2, 2, 3, 1)
Combination #89: (2, 2, 3, 2)
Combination #90: (2, 2, 3, 3)
Combination #91: (2, 2, 3, 4)
Combination #92: (2, 2, 4, 1)
Combination #93: (2, 2, 4, 2)
Combination #94: (2, 2, 4, 3)
Combination #95: (2, 2, 4, 4)
Combination #96: (2, 3, 1, 1)
Combination #97: (2, 3, 1, 2)
Combination #98: (2, 3, 1, 3)
Combination #99: (2, 3, 1, 4)
Combination #100: (2, 3, 2, 1)
Combination #101: (2, 3, 2, 2)
Combination #102: (2, 3, 2, 3)
Combination #103: (2, 3, 2, 4)
Combination #104: (2, 3, 3, 1)
Combination #105: (2, 3, 3, 2)
Combination #106: (2, 3, 3, 3)
Combination #107: (2, 3, 3, 4)
Combination #108: (2, 3, 4, 1)
Combination #109: (2, 3, 4, 2)
Combination #110: (2, 3, 4, 3)
Combination #111: (2, 3, 4, 4)
Combination #112: (2, 4, 1, 1)
Combination #113: (2, 4, 1, 2)
Combination #114: (2, 4, 1, 3)
Combination #115: (2, 4, 1, 4)
Combination #116: (2, 4, 2, 1)
Combination #117: (2, 4, 2, 2)
Combination #118: (2, 4, 2, 3)
Combination #119: (2, 4, 2, 4)
Combination #120: (2, 4, 3, 1)
Combination #121: (2, 4, 3, 2)
Combination #122: (2, 4, 3, 3)
Combination #123: (2, 4, 3, 4)
Combination #124: (2, 4, 4, 1)
Combination #125: (2, 4, 4, 2)
Combination #126: (2, 4, 4, 3)
Combination #127: (2, 4, 4, 4)
Combination #128: (3, 1, 1, 1)
Combination #129: (3, 1, 1, 2)
Combination #130: (3, 1, 1, 3)
Combination #131: (3, 1, 1, 4)
Combination #132: (3, 1, 2, 1)
Combination #133: (3, 1, 2, 2)
Combination #134: (3, 1, 2, 3)
Combination #135: (3, 1, 2, 4)
Combination #136: (3, 1, 3, 1)
Combination #137: (3, 1, 3, 2)
Combination #138: (3, 1, 3, 3)
Combination #139: (3, 1, 3, 4)
Combination #140: (3, 1, 4, 1)
Combination #141: (3, 1, 4, 2)
Combination #142: (3, 1, 4, 3)
Combination #143: (3, 1, 4, 4)
Combination #144: (3, 2, 1, 1)
Combination #145: (3, 2, 1, 2)
Combination #146: (3, 2, 1, 3)
Combination #147: (3, 2, 1, 4)
Combination #148: (3, 2, 2, 1)
Combination #149: (3, 2, 2, 2)
Combination #150: (3, 2, 2, 3)
Combination #151: (3, 2, 2, 4)
Combination #152: (3, 2, 3, 1)
Combination #153: (3, 2, 3, 2)
Combination #154: (3, 2, 3, 3)
Combination #155: (3, 2, 3, 4)
Combination #156: (3, 2, 4, 1)
Combination #157: (3, 2, 4, 2)
Combination #158: (3, 2, 4, 3)
Combination #159: (3, 2, 4, 4)
Combination #160: (3, 3, 1, 1)
Combination #161: (3, 3, 1, 2)
Combination #162: (3, 3, 1, 3)
Combination #163: (3, 3, 1, 4)
Combination #164: (3, 3, 2, 1)
Combination #165: (3, 3, 2, 2)
Combination #166: (3, 3, 2, 3)
Combination #167: (3, 3, 2, 4)
Combination #168: (3, 3, 3, 1)
Combination #169: (3, 3, 3, 2)
Combination #170: (3, 3, 3, 3)
Combination #171: (3, 3, 3, 4)
Combination #172: (3, 3, 4, 1)
Combination #173: (3, 3, 4, 2)
Combination #174: (3, 3, 4, 3)
Combination #175: (3, 3, 4, 4)
Combination #176: (3, 4, 1, 1)
Combination #177: (3, 4, 1, 2)
Combination #178: (3, 4, 1, 3)
Combination #179: (3, 4, 1, 4)
Combination #180: (3, 4, 2, 1)
Combination #181: (3, 4, 2, 2)
Combination #182: (3, 4, 2, 3)
Combination #183: (3, 4, 2, 4)
Combination #184: (3, 4, 3, 1)
Combination #185: (3, 4, 3, 2)
Combination #186: (3, 4, 3, 3)
Combination #187: (3, 4, 3, 4)
Combination #188: (3, 4, 4, 1)
Combination #189: (3, 4, 4, 2)
Combination #190: (3, 4, 4, 3)
Combination #191: (3, 4, 4, 4)
Combination #192: (4, 1, 1, 1)
Combination #193: (4, 1, 1, 2)
Combination #194: (4, 1, 1, 3)
Combination #195: (4, 1, 1, 4)
Combination #196: (4, 1, 2, 1)
Combination #197: (4, 1, 2, 2)
Combination #198: (4, 1, 2, 3)
Combination #199: (4, 1, 2, 4)
Combination #200: (4, 1, 3, 1)
Combination #201: (4, 1, 3, 2)
Combination #202: (4, 1, 3, 3)
Combination #203: (4, 1, 3, 4)
Combination #204: (4, 1, 4, 1)
Combination #205: (4, 1, 4, 2)
Combination #206: (4, 1, 4, 3)
Combination #207: (4, 1, 4, 4)
Combination #208: (4, 2, 1, 1)
Combination #209: (4, 2, 1, 2)
Combination #210: (4, 2, 1, 3)
Combination #211: (4, 2, 1, 4)
Combination #212: (4, 2, 2, 1)
Combination #213: (4, 2, 2, 2)
Combination #214: (4, 2, 2, 3)
Combination #215: (4, 2, 2, 4)
Combination #216: (4, 2, 3, 1)
Combination #217: (4, 2, 3, 2)
Combination #218: (4, 2, 3, 3)
Combination #219: (4, 2, 3, 4)
Combination #220: (4, 2, 4, 1)
Combination #221: (4, 2, 4, 2)
Combination #222: (4, 2, 4, 3)
Combination #223: (4, 2, 4, 4)
Combination #224: (4, 3, 1, 1)
Combination #225: (4, 3, 1, 2)
Combination #226: (4, 3, 1, 3)
Combination #227: (4, 3, 1, 4)
Combination #228: (4, 3, 2, 1)
Combination #229: (4, 3, 2, 2)
Combination #230: (4, 3, 2, 3)
Combination #231: (4, 3, 2, 4)
Combination #232: (4, 3, 3, 1)
Combination #233: (4, 3, 3, 2)
Combination #234: (4, 3, 3, 3)
Combination #235: (4, 3, 3, 4)
Combination #236: (4, 3, 4, 1)
Combination #237: (4, 3, 4, 2)
Combination #238: (4, 3, 4, 3)
Combination #239: (4, 3, 4, 4)
Combination #240: (4, 4, 1, 1)
Combination #241: (4, 4, 1, 2)
Combination #242: (4, 4, 1, 3)
Combination #243: (4, 4, 1, 4)
Combination #244: (4, 4, 2, 1)
Combination #245: (4, 4, 2, 2)
Combination #246: (4, 4, 2, 3)
Combination #247: (4, 4, 2, 4)
Combination #248: (4, 4, 3, 1)
Combination #249: (4, 4, 3, 2)
Combination #250: (4, 4, 3, 3)
Combination #251: (4, 4, 3, 4)
Combination #252: (4, 4, 4, 1)
Combination #253: (4, 4, 4, 2)
Combination #254: (4, 4, 4, 3)
Combination #255: (4, 4, 4, 4)
Python made this super easy, and I almost feel guilty even taking the time to blog about this when I didn’t have to do any real work to make it happen! Just the same, I’ll still sign off with Happy coding!