Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active January 1, 2017 06:25
Show Gist options
  • Save abiodun0/80bc65f30b9ff7dcfc06c555dccc26e9 to your computer and use it in GitHub Desktop.
Save abiodun0/80bc65f30b9ff7dcfc06c555dccc26e9 to your computer and use it in GitHub Desktop.
Highest population
def most_populated(births, deaths):
b = dict((i, births.count(i)) for i in births)
d = dict((i, deaths.count(i)) for i in deaths)
alive = 0
years = {}
for year in range(min(births), max(deaths)):
alive = alive + b.get(year, 0) - d.get(year, 0)
years[year] = alive
max_year = max(years.values())
return [key for key, val in years.iteritems() if val == max_year]
births = [18, 0, 10, 4, 16, 2, 16]
deaths = [30, 12, 22, 19, 28, 20, 28]
print most_populated(births, deaths)
# Given a group of people with their birth years and death years, find the year with the highest population.
# *Input*: an array of _n_ birth years, `births`, an array of _n_ death years, `deaths` corresponding to the person's birth year.
# *Required output*: an array, `years`, containing the year(s) with the highest population
# births [18, 0, 10, 4, 16, 2, 16]
# deaths [30, 12, 22, 19, 28, 20, 28]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment