Skip to main content
deleted 18 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:

def testmax():
    years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

    endyr = max([deathyr for (birthyr,deathyr) in years])
    startyr = min([birthyr for (birthyr,deathyr) in years])
    print(startyr, endyr) # correctly returns 1804 1997

As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = 0O(N).

I can improve this with a simple function that is longer but does it through one iteration of the years list - I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).

My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.

Many thanks!

A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:

def testmax():
    years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

    endyr = max([deathyr for (birthyr,deathyr) in years])
    startyr = min([birthyr for (birthyr,deathyr) in years])
    print(startyr, endyr) # correctly returns 1804 1997

As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = 0(N).

I can improve this with a simple function that is longer but does it through one iteration of the years list - I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).

My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.

Many thanks!

A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:

def testmax():
    years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

    endyr = max([deathyr for (birthyr,deathyr) in years])
    startyr = min([birthyr for (birthyr,deathyr) in years])
    print(startyr, endyr) # correctly returns 1804 1997

As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = O(N).

I can improve this with a simple function that is longer but does it through one iteration of the years list I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).

My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.

Source Link
rishijd
  • 203
  • 2
  • 6

Getting a min birth year and max death year from a list of years

A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:

def testmax():
    years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

    endyr = max([deathyr for (birthyr,deathyr) in years])
    startyr = min([birthyr for (birthyr,deathyr) in years])
    print(startyr, endyr) # correctly returns 1804 1997

As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = 0(N).

I can improve this with a simple function that is longer but does it through one iteration of the years list - I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).

My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.

Many thanks!