Created
November 24, 2022 19:18
-
-
Save tirinox/098bf47477ca1edf036988a0bd065898 to your computer and use it in GitHub Desktop.
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
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