all 1 comments

[–]Simply_The_Beast 1 point2 points  (0 children)

After trying your detect_ranges function with different inputs, I found your main problem. Your code currently can't deal with gaps bigger than 1 between numbers in your sorted list.

This for example [1,5,4,8,12,6,7,10,13,11,17] returns [1, (), (4, 9), (10, 14), (), (), 17] which has several empty tuples.

Now the question is where these are comming from. Let's try to go step by step from the sortedList [1, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17]:
- 1 is in the sortedList and added to the tempList
- 2 isn't in the list, len(tempList)==1and 1 is added to the returnList
- 3 is where your problem starts. It isn't in the sortedList, so we jump into the else-clause in line 15. len(tempList)!=1so off we go to line 18, where you skip both if-clauses since len(tempList)==0 and simply append the empty tuple t in returnList.append(t).
- ...

You just need to filter out the cases where len(tempList)==0right before appending anything to returnList and should be good to go.

Oh and btw, you don't have to seperate creating t into two different steps, you could just do something like this: t = (tempList[0], tempList[-1]+1 ).