Skip to content

Instantly share code, notes, and snippets.

@watersb
Created June 17, 2015 03:33
Show Gist options
  • Save watersb/642af9deaef997bd0570 to your computer and use it in GitHub Desktop.
Save watersb/642af9deaef997bd0570 to your computer and use it in GitHub Desktop.
year with most alive.py
from collections import defaultdict
demographic_data = '''
James 1954 1977
Linda 1922 1928
Robert 1912 1924
Mary 1965 1993
John 1915 1915
Patty 1946 2014
Michael 1939 2004
Barbara 1961 1991
David 1949 1962
Susan 1967 2014
William 1919 1967
Nancy 1939 2002
Richard 1944 1953
Deborah 1967 2034
Thomas 1923 1973
Sandra 1945 1965
Charles 1918 1986
Carol 1913 1921
Gary 1934 1980
Kate 1917 1929
Larry 1964 2014
Sharon 1959 1964
Ronald 1909 1936
Karen 1916 1981
Joseph 1925 1941
Donna 1905 1908
Donald 1969 1996
Brenda 1941 1967
Kenneth 1954 1961
Magie 1909 1933
Steven 1955 1967
Diane 1961 2023
Dennis 1920 1933
Pamela 1939 1976
Paul 1914 1982
Janet 1945 1971
Stephen 1900 1917
Shirley 1937 2003
George 1943 1992
Carolyn 1958 2005
'''
alive = defaultdict(int)
for data in demographic_data.splitlines():
if len(data):
birth, death = (int(x) for x in data.split()[1:])
for year in range(birth , death):
alive[year] += 1
######
max_year = max(alive, key=alive.get)
print "In %i, the maximum of %i people were alive." % \
(max_year, alive[max_year])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment