Skip to content

Instantly share code, notes, and snippets.

@Microserf
Created April 22, 2017 22:05
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 Microserf/3f90bad8817a7a41be648e41cc8b8046 to your computer and use it in GitHub Desktop.
Save Microserf/3f90bad8817a7a41be648e41cc8b8046 to your computer and use it in GitHub Desktop.
Aging calculator
#!/usr/bin/env python3
from datetime import datetime
TARGETS = {
'Anthony': {
'dob': datetime(1982, 10, 19),
'aging_rate': 1
},
'Winnie': {
'dob': datetime(2012, 5, 18),
'aging_rate': 7
}
}
def find_intersect(profileA, profileB):
lhs = profileA['aging_rate'] - profileB['aging_rate']
rhs = profileA['aging_rate'] * profileA['dob'].timestamp() - profileB['aging_rate'] * profileB['dob'].timestamp()
x_value = datetime.fromtimestamp(rhs / lhs)
y_value = make_fx(profileA)(x_value)
return (x_value, y_value)
def human_readable(age):
minute_length = 60
hour_length = 60 * minute_length
day_length = 24 * hour_length
year_length = 365.25 * day_length
years = int(age / year_length)
age -= years * year_length
days = int(age / day_length)
age -= days * day_length
hours = int(age / hour_length)
age -= hours * hour_length
minutes = int(age / minute_length)
return '{0}, {1}, {2} and {3}'.format(
pluralize(years, 'year'),
pluralize(days, 'day'),
pluralize(hours, 'hour'),
pluralize(minutes, 'minute')
)
def make_fx(aging_profile):
return lambda x: int(aging_profile['aging_rate'] * (x.timestamp() - aging_profile['dob'].timestamp()))
def pluralize(number, word, plural='s'):
return '{0} {1}{2}'.format(number, word, ('' if number == 1 else plural))
if __name__ == '__main__':
for (name, aging_profile) in TARGETS.items():
# let y represent age
# let x represent date
# let m represent aging rate
# let b represent date of birth
#
# y = m(x - b)
age = make_fx(aging_profile)
now_age = age(datetime.now())
print('{0}\'s age now is: {1}'.format(name, human_readable(now_age)))
names = ' and '.join(TARGETS.keys())
intersection = find_intersect(*TARGETS.values())
print('{0}\'s age will intersect on {1}. They will both be {2} old.'.format(names, intersection[0], human_readable(intersection[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment