Dec-14-2022, 03:41 PM
(Dec-14-2022, 02:40 PM)deanhystad Wrote: Please wrap posted code in Python tags.Thanks for the head up, corrected.
(Dec-14-2022, 02:40 PM)deanhystad Wrote: I don't see how a binary search is applicable. A binary search would work if page_exists(k) returned False when k >= number of pages, but it doesn't sound like that is what it does. Instead, if I understand you correctly, page_exists(k) returns False if k >= number of pages or if page[k] doesn't have any adds.Thanks again, I understand the reference to binary search algorithm was wrong.
(Dec-14-2022, 02:40 PM)deanhystad Wrote: The code below demonstrates the problem. last_page() finds the last page when pages are contiguous, but when I make a list of pages that has holes, it can grab the last page before one of the holes.Pages are always contiguous (update the main post, I forgot to report that in the first place); so no problem with the "holes", as there are any in this case.
In the non-contiguous data set, last page found the hole between 10 and 14 and decide that was the end of pages.
Your implementation of a binary search is also incorrect. The end condition is not test == 0, it is test == last page. The way to know that you found the last page is max = min + 1.
Algorithms are still a new thing for me, so this helps a lot for the future. Thank you, I understand where the problem was.