Philip's just turned four and he wants to know how old he will be in various years in the future such as 2090 or 3044. His parents can't keep up calculating this so they've begged you to help them out by writing a programme that can answer Philip's endless questions.

Your task is to write a function that takes two parameters: the year of birth and the year to count years in relation to. As Philip is getting more curious every day he may soon want to know how many years it was until he would be born, so your function needs to work with both dates in the future and in the past.

Provide output in this format: For dates in the future: "You are ... year(s) old." For dates in the past: "You will be born in ... year(s)." If the year of birth equals the year requested return: "You were born this very year!"

"..." are to be replaced by the number, followed and proceeded by a single space. Mind that you need to account for both "year" and "years", depending on the result.

Good Luck!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def calculate_age(year_of_birth, current_year):
	

Solutions:
def calculate_age(year_of_birth, current_year):
	diff = abs(current_year - year_of_birth)
	plural = '' if diff == 1 else 's'
	if year_of_birth < current_year:
		return 'You are {} year{} old.'.format(diff, plural)
	elif year_of_birth > current_year:
		return 'You will be born in {} year{}.'.format(diff, plural)
	return 'You were born this very year!'


def calculate_age(birth, curr):
	diff = abs(curr - birth)
	age = '%d year%s' % (diff, 's' * bool(diff - 1))
	return 'You were born this very year!' if not diff else 'You are %s old.' % age if curr > birth else 'You will be born in %s.' % age


def calculate_age(birth_year, this_year):
	(results, plural) = (this_year - birth_year, '')
	if abs(results) != 1:
		plural = 's'
	if results > 0:
		return 'You are %s year%s old.' % (results, plural)
	elif results < 0:
		return 'You will be born in %s year%s.' % (abs(results), plural)
	elif results == 0:
		return 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	year = year_of_birth - current_year
	c1 = f"You are {abs(year)} {['year', 'years'][abs(year) != 1]} old."
	c2 = f"You will be born in {year} {['year', 'years'][year != 1]}."
	return 'You were born this very year!' if not year else [c1, c2][year > 0]


def calculate_age(dob, now):
	return f"You will be born in {dob - now} {('years' if dob - now > 1 else 'year')}." if dob > now else 'You were born this very year!' if dob == now else f"You are {now - dob} {('years' if now - dob > 1 else 'year')} old."


def calculate_age(year_of_birth, current_year):
	a = abs(current_year - year_of_birth)
	if not a:
		return 'You were born this very year!'
	else:
		return (f"You are {a} year{('', 's')[a > 1]} old.", f"You will be born in {a} year{('', 's')[a > 1]}.")[current_year < year_of_birth]


def calculate_age(year_of_birth, current_year):
	diff = current_year - year_of_birth
	s = 's' if abs(diff) > 1 else ''
	return f'You are {diff} year{s} old.' if diff > 0 else f'You will be born in {-diff} year{s}.' if diff < 0 else 'You were born this very year!'


def calculate_age(birth, current):
	total = abs(current - birth)
	plural = 's' if total > 1 else ''
	if birth == current:
		return 'You were born this very year!'
	return 'You will be born in {} year{}.'.format(total, plural) if birth > current else 'You are {} year{} old.'.format(total, plural)


calculate_age = lambda b, c: ['You will be born in {} year{}.', 'You were born this very year!', 'You are {} year{} old.'][(c - b >= 0) + (c - b > 0)].format(abs(c - b), 's' if abs(c - b) != 1 else '')


def calculate_age(yob, cy):
	return ['You will be born in %s %s.' % (yob - cy, ['years', 'year'][yob - cy < 2]), 'You are %s %s old.' % (cy - yob, ['years', 'year'][cy - yob < 2])][cy - yob > 0] if cy - yob != 0 else 'You were born this very year!'


calculate_age = lambda b, y: (lambda d: 'You are %s year%s old.' % (d, 's' if d != 1 else '') if d > 0 else 'You will be born in %s year%s.' % (-d, 's' if d != -1 else '') if d < 0 else 'You were born this very year!')(y - b)


def calculate_age(year_of_birth: int, current_year: int) -> str:
	age = current_year - year_of_birth
	if age > 0:
		return f"You are {age} {('year' if age == 1 else 'years')} old."
	elif age < 0:
		return f"You will be born in {abs(age)} {('year' if abs(age) == 1 else 'years')}."
	else:
		return 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	dif = year_of_birth - current_year
	return 'You ' + ['are {} year{} old.', 'will be born in {} year{}.'][dif > 0].format(abs(dif), 's' * (abs(dif) != 1)) if dif else 'You were born this very year!'


def calculate_age(b, c):
	return 'You are {} {} old.'.format(c - b, 'years' if c - b > 1 else 'year') if c > b else 'You will be born in {} {}.'.format(b - c, 'years' if b - c > 1 else 'year') if b > c else 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	if current_year - year_of_birth > 0:
		return 'You are %d year%s old.' % (current_year - year_of_birth, '' if current_year - year_of_birth == 1 else 's')
	elif current_year - year_of_birth < 0:
		return 'You will be born in %d year%s.' % (year_of_birth - current_year, '' if year_of_birth - current_year == 1 else 's')
	else:
		return 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	diff = current_year - year_of_birth
	if diff == 0:
		return 'You were born this very year!'
	elif diff == 1:
		return 'You are {} year old.'.format(diff)
	elif diff > 1:
		return 'You are {} years old.'.format(diff)
	elif diff == -1:
		return 'You will be born in {} year.'.format(abs(diff))
	elif diff < -1:
		return 'You will be born in {} years.'.format(abs(diff))


def calculate_age(year_of_birth, current_year):
	years = abs(year_of_birth - current_year)
	if years == 1 and year_of_birth < current_year:
		return 'You are 1 year old.'
	if years == 1 and year_of_birth > current_year:
		return 'You will be born in 1 year.'
	return f'You are {years} years old.' if year_of_birth < current_year else f'You will be born in {years} years.' if year_of_birth > current_year else 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	ans = current_year - year_of_birth
	if ans == 0:
		ans = 'You were born this very year!'
	elif ans == 1:
		ans = 'You are 1 year old.'
	elif ans < 1 and ans != -1:
		ans = 'You will be born in ' + str(ans * -1) + ' years.'
	elif ans == -1:
		ans = 'You will be born in 1 year.'
	else:
		ans = 'You are ' + str(ans) + ' years old.'
	return ans


def calculate_age(year_of_birth, current_year):
	difference = current_year - year_of_birth
	if difference == 1:
		return 'You are 1 year old.'
	if difference == -1:
		return 'You will be born in 1 year.'
	if difference > 0:
		return 'You are {difference} years old.'.format(difference=difference)
	if difference == 0:
		return 'You were born this very year!'
	else:
		difference = abs(difference)
		return 'You will be born in {difference} years.'.format(difference=difference)


def calculate_age(yob, cy):
	if cy == yob + 1:
		return 'You are 1 year old.'
	if yob == cy + 1:
		return 'You will be born in 1 year.'
	return 'You were born this very year!' if yob == cy else f'You are {cy - yob} years old.' if cy > yob else f'You will be born in {yob - cy} years.'


def calculate_age(b, c):
	return f"You will be born in {(str(b - c) + ' year' if b - c <= 1 else str(b - c) + ' years')}." if c - b < 0 else f'You were born this very year!' if c == b else f"You are {(str(c - b) + ' year' if c - b <= 1 else str(c - b) + ' years')} old."


def calculate_age(year_of_birth, current_year):
	age = current_year - year_of_birth
	text = 'You were born this very year!'
	if age > 0:
		text = f"You are {age} year{'s' * (age > 1)} old."
	elif age < 0:
		text = f"You will be born in {abs(age)} year{'s' * (abs(age) > 1)}."
	return text


def calculate_age(y, cy):
	r = cy - y
	y = 'year' + ((abs(r) > 1 or '') and 's')
	return f'You are {r} {y} old.' if r > 0 else f'You will be born in {r * -1} {y}.' if r < 0 else 'You were born this very year!'


def calculate_age(year_of_birth: int, current_year: int) -> str:

	def year_str(y: int) -> str:
		return '1 year' if y == 1 else str(y) + ' years'
	delta = current_year - year_of_birth
	if delta == 0:
		return 'You were born this very year!'
	elif delta > 0:
		return 'You are ' + year_str(delta) + ' old.'
	else:
		return 'You will be born in ' + year_str(-delta) + '.'


def calculate_age(year_of_birth, current_year):
	age = abs(year_of_birth - current_year)
	if year_of_birth < current_year:
		if age == 1:
			return 'You are {} year old.'.format(current_year - year_of_birth)
		return 'You are {} years old.'.format(current_year - year_of_birth)
	elif year_of_birth > current_year:
		if age == 1:
			return 'You will be born in {} year.'.format(year_of_birth - current_year)
		return 'You will be born in {} years.'.format(year_of_birth - current_year)
	else:
		return 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	diff = current_year - year_of_birth
	if diff > 1:
		return f'You are {diff} years old.'
	elif diff < -1:
		return f'You will be born in {-diff} years.'
	elif diff == -1:
		return f'You will be born in {-diff} year.'
	elif diff == 1:
		return f'You are {diff} year old.'
	else:
		return 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	x = year_of_birth
	y = current_year
	if x < y:
		if y - x == 1:
			return 'You are 1 year old.'
		else:
			w = str(y - x)
			return 'You are ' + w + ' years old.'
	elif x == y:
		return 'You were born this very year!'
	elif x > y:
		if x - y == 1:
			return 'You will be born in 1 year.'
		else:
			k = str(x - y)
			return 'You will be born in ' + k + ' years.'


def calculate_age(a, b):
	if abs(a - b) > 1:
		if b > a:
			return 'You are ' + str(b - a) + ' years old.'
		elif b < a:
			return 'You will be born in ' + str(a - b) + ' years.'
	elif a - b == 0:
		return 'You were born this very year!'
	elif b - a == 1:
		return 'You are 1 year old.'
	elif a - b == 1:
		return 'You will be born in 1 year.'


def calculate_age(year_of_birth, current_year):
	x = year_of_birth - current_year
	if x > 1:
		return 'You will be born in ' + str(x) + ' years.'
	if x < -1:
		return 'You are ' + str(-1 * x) + ' years old.'
	if x == 0:
		return 'You were born this very year!'
	if x == -1:
		return 'You are 1 year old.'
	if x == 1:
		return 'You will be born in 1 year.'


def calculate_age(y, c):
	year = 'years'
	if c + 1 == y or c - 1 == y or c == y:
		year = 'year'
	return 'You are ' + str(c - y) + ' ' + year + ' old.' if c > y else 'You will be born in ' + str(y - c) + ' ' + year + '.' if y > c else 'You were born this very ' + year + '!'


def calculate_age(year_of_birth, current_year):
	if year_of_birth < current_year:
		yearsOld = current_year - year_of_birth
		if yearsOld == 1:
			return 'You are ' + str(yearsOld) + ' year old.'
		else:
			return 'You are ' + str(yearsOld) + ' years old.'
	elif year_of_birth > current_year:
		yearsOld = year_of_birth - current_year
		if yearsOld == 1:
			return 'You will be born in ' + str(yearsOld) + ' year.'
		else:
			return 'You will be born in ' + str(yearsOld) + ' years.'
	elif year_of_birth == current_year:
		yearsOld = year_of_birth
		return 'You were born this very year!'


def calculate_age(y, c):
	if c - y == 1:
		return f'You are 1 year old.'
	if y - c == 1:
		return f'You will be born in 1 year.'
	if c > y:
		return f'You are {c - y} years old.'
	if c < y:
		return f'You will be born in {y - c} years.'
	if c == y:
		return 'You were born this very year!'


def calculate_age(b, c):
	d = c - b
	return (d > 0) * f"You are {d} year{'s' * (d > 1)} old." + (d < 0) * f"You will be born in {-d} year{'s' * (d < -1)}." + (d == 0) * 'You were born this very year!'


def calculate_age(year_of_birth, current_year):
	y = current_year - year_of_birth
	if y > 0:
		return 'You are {} {} old.'.format(y, 'years' if y != 1 else 'year')
	elif y == 0:
		return 'You were born this very year!'
	else:
		return 'You will be born in {} {}.'.format(-y, 'years' if y != -1 else 'year')


def calculate_age(year_of_birth, current_year):
	age = current_year - year_of_birth
	if age == 1:
		return 'You are %s year old.' % age
	if age > 1:
		return 'You are %s years old.' % age
	if age == 0:
		return 'You were born this very year!'
	if age == -1:
		return 'You will be born in %s year.' % abs(age)
	else:
		return 'You will be born in %s years.' % abs(age)


def calculate_age(year_of_birth, current_year):
	years = current_year - year_of_birth
	if not years:
		return 'You were born this very year!'
	elif years > 0:
		return f"You are {years} year{('' if years == 1 else 's')} old."
	return f"You will be born in {-years} year{('' if years == -1 else 's')}."


def calculate_age(birthyear, target):
	if birthyear == target:
		return 'You were born this very year!'
	elif birthyear > target:
		return 'You will be born in {} year{}.'.format(birthyear - target, 's' * (birthyear - target > 1))
	else:
		return 'You are {} year{} old.'.format(target - birthyear, 's' * (target - birthyear > 1))


def calculate_age(b, c):
	greet = ['You will be born in {} year{}.', 'You are {} year{} old.', 'You were born this very year!']
	return greet[2] if b == c else greet[b < c].format(abs(b - c), 's' if abs(b - c) != 1 else '')