[Tutor] Finding the max value from a dictionary that does not exceed a variable's value.

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Feb 1 07:47:02 EST 2016


On 31 January 2016 at 05:50, srinivas devaki <mr.eightnoteight at gmail.com> wrote:
> On Sun, Jan 31, 2016 at 3:54 AM, Danny Yoo <dyoo at hashcollision.org> wrote:
>> ---
>> I want to take the max value in the dictionary 'coinvalues'  that is the
>> same as or lower than the variable 'change'.  I have no idea how to search
>> through the 'coinvalues' dictionary and find the value that is highest but
>> does not exceed the value held in the variable 'change'.
>> ---
>
> although OP's problem doesn't need this, is there a better way achieve
> this other than
> using a balanced binary search tree.

You would need to state all of the requirements for your data
structure. If coinvalues is constant then you can use a list and the
bisect module:

https://docs.python.org/3.5/library/bisect.html

That gives O(log(len(coinvalues))) lookup.

--
Oscar


More information about the Tutor mailing list