Skip to content

Instantly share code, notes, and snippets.

@isaackogan
Last active August 23, 2021 21:31
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 isaackogan/6d6af047833a950090673dc151de5cd3 to your computer and use it in GitHub Desktop.
Save isaackogan/6d6af047833a950090673dc151de5cd3 to your computer and use it in GitHub Desktop.
[Python] Milestone Caculation
from typing import Optional
def roundup(x: int, tens: int) -> int:
val: int = int("1" + ("0" * tens))
return x if x % val == 0 else x + val - x % val
def halfway(x: int) -> int:
val: int = int("5" + ("0" * (len(str(x)) - 2)))
return val
def get_milestone(before: int, after: int) -> Optional[int]:
"""
Calculate milestone with two integers
:param before: Value from previous check
:param after: Value at current check
:return: Whether or not it's a milestone
Milestone Pattern:
10 | 15 | 20 | 25
100 | 150 | 200 | 250
1,000 | 1,500 | 2,000 | 2,500 ... 30, 35, etc...
10,000 | 10,500 | 20,000 | 20,500
100,000 | 150,000 | 200,000 | 250,000
1,000,000 | 1,500,000 | 2,000,000 | 2,500,000
... Continues infinitely
"""
# If it's smaller or equal, it can't be a milestone
if after < before or after == before:
return None
# Less than 10 manual
if before < 10 and after > 10:
return 10
before = before + 1 if (before % 10 == 0 or before % 5 == 0) else before
rounded_up: int = roundup(before, len(str(round(before))) - 1)
rounded_up_half: int = rounded_up - halfway(rounded_up)
# Hit a halfway milestone
if after >= rounded_up_half and not before > rounded_up_half:
return rounded_up_half
# Hit a tens/thousands/etc. milestone
if after >= rounded_up:
return rounded_up
# There is no milestone
return None
@isaackogan
Copy link
Author

Can be used to calculate any milestone for incrementing forward values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment