Skip to content

Instantly share code, notes, and snippets.

@rwhelan
Created September 5, 2022 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwhelan/00cdf1298b1e4862ba6cbe089c324871 to your computer and use it in GitHub Desktop.
Save rwhelan/00cdf1298b1e4862ba6cbe089c324871 to your computer and use it in GitHub Desktop.
barbell_weight = 45
# weight_selection = [45, 35, 25, 10, 5, 5, 2.5]
weight_selection = [55, 45, 35, 25, 15, 15, 10, 5, 2.5]
max_possible_weight = int(sum(weight_selection) * 2 + barbell_weight)
class ImpossibleWeight(Exception):
def __init__(self, weight):
self.weight = weight
def calculate_weights(desired_weight):
if desired_weight < barbell_weight:
raise Exception(f"desired weight must be equal to or greater than barbell: {barbell_weight}")
if desired_weight > max_possible_weight:
raise Exception(f"{desired_weight} greater than computable max")
if desired_weight % 5 != 0:
raise Exception("Value must be multiple of 5")
needed_weights = []
remaining_weight = (desired_weight - barbell_weight) / 2.0
for weight in weight_selection:
if weight <= remaining_weight:
needed_weights.append(weight)
remaining_weight -= weight
if remaining_weight != 0:
raise ImpossibleWeight(desired_weight)
return needed_weights
for w in range(barbell_weight, max_possible_weight+5, 5):
try:
print(f'"{w}","{", ".join([str(i) for i in calculate_weights(w)])}"')
except ImpossibleWeight:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment