Last active
March 20, 2024 23:12
-
-
Save crackwitz/5ff9def054da92fe2c3ffd96e59b12c6 to your computer and use it in GitHub Desktop.
cosmetics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import time | |
from itertools import * | |
import pprint; pp = pprint.pprint | |
def inclusive_range(smallest, largest, step): | |
N = 1 + int(round((largest - smallest) / step)) | |
return [smallest + k*step for k in range(N)] | |
decimals = 4 | |
factor = 10**decimals | |
blocks = [] | |
blocks += inclusive_range(.1001, .1009, .0001) | |
blocks += inclusive_range(.101, .149, .001) | |
blocks += inclusive_range(.050, .950, .050) | |
blocks += [1,2,3,4] | |
blocks = [int(round(v*factor)) for v in blocks] # don't wanna calculate with floats | |
blocks.sort(reverse=True) # better time, moves most additions to the end | |
# collect sum lengths and shortest combinations | |
# this will contain the combination of NO blocks, nicer to deal with | |
solutions = {0: ()} # {sum: list} | |
ta = time.perf_counter() | |
for i,block in enumerate(blocks): | |
# add this block to previous solutions | |
t0 = time.perf_counter() | |
updates = { | |
(Sum + block): List + (block,) | |
for Sum,List in solutions.items() | |
if Sum+block not in solutions | |
or len(List)+1 < len(solutions[Sum+block]) | |
} | |
solutions.update(updates) | |
t1 = time.perf_counter() | |
print(f"{i+1}: {block/factor:.{decimals}f} -> {len(solutions)} combinations, {t1-t0:.3f}s") | |
tb = time.perf_counter() | |
print(f"time: {tb-ta:.3f}s") | |
print("shortest sums:", sorted(solutions.keys())[:5]) # [0, 500, 1000, 1001, 1002] | |
print("longest sums:", sorted(solutions.keys())[-5:]) # [264293, 264294, 264295, 264795, 265295] | |
# example | |
print("3.9762 =", solutions[39762]) # (1002, 1260, 7500, 30000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment