나는 이렇게 학습한다/Algorithm & SQL

0919. Cat years, Dog years

daco2020 2022. 9. 19. 23:49
반응형

Kata Task

I have a cat and a dog.

I got them at the same time as kitten/puppy. That was humanYears years ago.

Return their respective ages now as [humanYears,catYears,dogYears]

NOTES:

  • humanYears >= 1
  • humanYears are whole numbers only

Cat Years

  • 15 cat years for first year
  • +9 cat years for second year
  • +4 cat years for each year after that

Dog Years

  • 15 dog years for first year
  • +9 dog years for second year
  • +5 dog years for each year after that



Solution:

CAT_INTERVAL = 4
DOG_INTERVAL = 5


def human_years_cat_years_dog_years(years):
    cat_years = _get_years(CAT_INTERVAL, years)
    dog_years = _get_years(DOG_INTERVAL, years)
    return [years, cat_years, dog_years]

def _get_years(interval, years):
    age_table = {1: 15, 2: 24}
    age_table.update({i: (i-2)*interval + 24 for i in range(3, years+1)})
    return age_table[years]


반응형