Python Code Stuff
Gage Block Buildup
Here is a python script for taking a list of gage blocks and finding all of the combinations you can use to get there. I did this when I was thinking about incomplete gage block sets that have been really pilfered. Could you still reach certain values if you were missing blocks? Of course, I’ve limited it to a specific number of max results since even in a small 36 pc set the combinations go up into the 100’s or more. For example, the size I have in code here at .470 in the large gage block list is a little over 2300 results. Play around with it.
def gage_blocks(blocks, buildup, maxResults):
blocks.sort(reverse=True) #if this isn't set to true you're asking for the worst values and huge run times
def helper(remaining, current, result, start):
remaining = round(remaining, 4)
if remaining < 0 or len(result) >= maxResults:
return
elif remaining == 0:
result.append(current)
return
for i in range(start, len(blocks)):
block = blocks[i]
new_current = current[:]
new_current.append(block)
helper(remaining - block, new_current, result, i + 1)
result = []
helper(buildup, [], result, 0)
return result[:maxResults]
blk_small_inch=[0.05, 0.1001, 0.1002, 0.1003, 0.1004, 0.1005, 0.1006, 0.1007, 0.1008, 0.1009, 0.101, 0.102, 0.103, 0.104, 0.105, 0.106, 0.107, 0.108, 0.109, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 2, 4]
blk_large_inch = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 0.1001, 0.1002, 0.1003, 0.1004, 0.1005, 0.1006, 0.1007, 0.1008, 0.1009, 0.101, 0.102, 0.103, 0.104, 0.105, 0.106, 0.107, 0.108, 0.109, 0.11, 0.111, 0.112, 0.113, 0.114, 0.115, 0.116, 0.117, 0.118, 0.119, 0.12, 0.121, 0.122, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128, 0.129, 0.13, 0.131, 0.132, 0.133, 0.134, 0.135, 0.136, 0.137, 0.138, 0.139, 0.14, 0.141, 0.142, 0.143, 0.144, 0.145, 0.146, 0.147, 0.148, 0.149, 1, 2, 3, 4]
blk_small_metric = [1.005, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 50, 75, 100]
res=gage_blocks(blk_large_inch, .470, 5)
print(res)
print(len(res))