Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
... which gets even simpler with η-reduction to just (thanks @Gareth @Gareth!):
max(paths, key=len)
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
... which gets even simpler with η-reduction to just (thanks @Gareth!):
max(paths, key=len)
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
... which gets even simpler with η-reduction to just (thanks @Gareth!):
max(paths, key=len)
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
... which gets even simpler with η-reduction to just (thanks @Gareth !):
max(paths, key=len)
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
... which gets even simpler with η-reduction to just (thanks @Gareth !):
max(paths, key=len)
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
A simple pythonic way to achieveYou can code this isalgorithm yourself,
and in many programming languages you would have to use the.
In Python,
the max
built-in takes a key
parameter of the,
to perform such "max by" operation.
The maxkey
builtparameter must be a one-inargument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
A simple pythonic way to achieve this is to use the key
parameter of the max
built-in:
max(paths, key=lambda coll: len(coll))
Note that your code iterates over paths
twice: once to calculate the max length,
and one more time to find all elements that have the max length.
It would be better to iterate only once, keeping track of the max value, and the item that had the max value. At the end of the iteration (single pass), return the item that had the max value.
You can code this algorithm yourself,
and in many programming languages you would have to.
In Python,
the max
built-in takes a key
parameter,
to perform such "max by" operation.
The key
parameter must be a one-argument function.
For each item in the collection,
the function will receive the item,
and should return a value that will be used as the rank of the item.
In your example, the rank of each item is its length. That gives this simple pythonic code:
max(paths, key=lambda coll: len(coll))