Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Binary Search to Sort a List in Python
    primarykey
    data
    text
    <p>My question accepts a (listof plane) structure where a plane is a list [airplane_code, date, price]:</p> <ul> <li>airplane_code is the plane name</li> <li>date is 1-365 inclusive where all dates might not be unique for each plane</li> <li>price is int[>=0] where all prices are unique for each plane</li> </ul> <p>The function also produces a (listof plane). I need to filter this list according to the two other variable my function accepts (start_date and end_date) first and then by price (in ascending order). However I am restricted to using the <strong>binary search</strong> concept to sort for <strong>price</strong>: </p> <pre><code>def binary_search(lst, target): beginning = ... end = ... while ...: middle = ... if lst[middle] &lt; target: ... ##update beginning and end else: ... ##update beginning and end </code></pre> <p>I cannot figure out how binary search would allow me to sort a list and would appreciate any help. This is what I have done until now (filtering for date variables given):</p> <pre><code>def determine(planes, start_date, end_date): correct_planes = [] plane_price = [] final_selection = [] for plane in planes: if plane[1] &gt;= start_date and plane[1] &lt;= end_date: correct_planes.append(plane) plane_price.append(plane[2]) </code></pre> <p>An example of how the function works:</p> <blockquote> <p>plane_list = [['A11', 215, 300], ['A22', 260, 750], ['A33', 230, 600], ['A44', 300, 400]]</p> <p>determine(plane_list, 200, 260) => [['A11', 215, 300], ['A33', 260, 600], ['A22', 260, 750]]</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload