Hash maps and sets · stretch
Longest run of consecutive numbers
Write longest_run(nums) returning the length of the longest run of consecutive whole numbers in the list, in any order: [100, 4, 200, 1, 3, 2] has 1, 2, 3, 4, so 4.
Put everything in a set. A run starts at a number whose predecessor is missing; count up from there.
- right answers
- arguments left as they should be
- fast enough at scale
- without sorted, sort
Run adds print(longest_run([100, 4, 200, 1, 3, 2])) after your code, to try it.
Stuck on the idea rather than the code? The Choosing a key lesson walks through it.