Created
November 27, 2019 05:42
-
-
Save selenologist/ce6dafc155790152a6a726ec9f0c41d1 to your computer and use it in GitHub Desktop.
Simple brute force of ideal amount of credits to add to OVH public cloud that minimises wasted credits with a monthly service
This file contains 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
# OVH only lets you add integer values of credits at a time, | |
# but credits expire 13 months after being purchased. | |
# So, given the monthly cost of the service and a current balance of credits | |
# (possibly smaller than the monthly cost) there exists an amount of added credits | |
# that will yield the lowest amount of leftover credits when the the balance becomes | |
# lower than the monthly cost. | |
# | |
# There's probably a smarter way to do this but this is computationally cheap enough that I | |
# really don't care. | |
import math # needed for math.ceil/math.floor | |
# current balance (any leftover credits you currently have): | |
CUR_BAL = 0.00 | |
# monthly cost: | |
MONTH_COST = 4.34 | |
# range of added integer credits to score. | |
# no point scoring values lasting less than 3 months (because IMO I'd have to refill too often) | |
# nor exceeding 12 months (or any leftover credits will expire before they could be used) | |
CREDIT_RANGE = range(math.ceil(MONTH_COST*3 - CUR_BAL), math.ceil(MONTH_COST*13 - CUR_BAL)) | |
# original REPL one-liner for history's sake: | |
# print(sorted([(x, (x + CUR_BAL) / MONTH_COST) for x in CREDIT_RANGE], key=lambda a: a[1] % 1)) | |
# produce a list of tuples, where the first element is the number of credits to add, and the | |
# secondelement is leftover credits when the the balance falls below the monthly cost. | |
results = [(x, (x + CUR_BAL) % MONTH_COST) for x in CREDIT_RANGE] | |
# this list is then sorted (ascending) by the amount of leftover credits | |
results.sort(key=lambda a: a[1]) | |
# finally, print the results. | |
# the number of months this amount will provide before running out again is also calculated. | |
# only the most efficient value for a given amount of months will be printed, any less efficient | |
# versions for the same number of months will be skipped. | |
seen_months = set() | |
for (added, leftover) in results: | |
months = math.floor((added + CUR_BAL) / MONTH_COST) | |
# only print a value for this number of months if we haven't yet printed a more efficient one. | |
if months not in seen_months: | |
seen_months.add(months) | |
print("${:<3} credits will provide {:2} months with ${:<4.2f} left over."\ | |
.format(added, months, leftover)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment