2/41
Part 1 of 7 · SortingLearn and do
Sort once, and the questions get easy
Many coding questions are hard on data in the order it arrived, and easy on the same data sorted. After a sort, copies sit next to each other. Overlapping time windows sit next to each other. And the question every lookup asks, where does this value fall, takes about 20 looks on a million sorted items instead of a million.
times = [160, 100, 220]
print(sorted(times))
print(times)
[100, 160, 220]
[160, 100, 220]
sorted(times) built a new list. times itself didn't change.
result = times.sort()
print(result)
print(times)
result = times.sort(). What's in result?
Pick one to see what it prints.
Pick an answer above to see the rest