Skip to content

Instantly share code, notes, and snippets.

@tirinox
Created November 24, 2022 19:18
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 tirinox/098bf47477ca1edf036988a0bd065898 to your computer and use it in GitHub Desktop.
Save tirinox/098bf47477ca1edf036988a0bd065898 to your computer and use it in GitHub Desktop.
import math
import random
MILESTONE_DEFAULT_PROGRESSION = [1, 2, 5]
def milestone_nearest(x, before=True, progress=None):
progress = progress or MILESTONE_DEFAULT_PROGRESSION
x = int(x)
if x <= 0:
return MILESTONE_DEFAULT_PROGRESSION[0]
mag = 10 ** int(math.log10(x))
if before:
delta = -1
mag *= 10
else:
delta = 1
i = 0
while True:
step = progress[i]
y = step * mag
if before and x >= y:
return y
if not before and x < y:
return y
i += delta
if i < 0:
i = len(progress) - 1
mag //= 10
elif i >= len(progress):
i = 0
mag *= 10
def main():
before = False
pr = [1, 2, 5]
while True:
r = input('next?').strip()
if not r:
x = int(random.uniform(0, 1000))
else:
try:
x = int(r)
except ValueError:
print('enter number!')
continue
print(f'{x:<10} => {milestone_nearest(x, before, pr)}')
print('-' * 100)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment