python - Find the best next key in a dictionary which value fits in a constrain -


i have dictionary names:weights of cows.

cows = {'herman': 7, 'moo moo': 3, 'betsy': 9, 'lola': 2, 'milkshake': 2, 'florence': 2, 'henrietta': 9, 'maggie': 3, 'millie': 5, 'oreo': 6} 

i have create list of lists dictionary using greedy algorithm. each sub-list has constrain: weight limit 10. means, have select biggest cow weight first , find next cow fits in sub-list. example, right answer problem dictionary be:

[ ['betsy'], ['henrietta'], ['herman', 'moo moo'], ['oreo', 'maggie'], ['millie', 'florence', 'milkshake'], ['lola']] 

i'm trying create function find next best fit subtrip. example, if first element of sublist 'herman' (weight of 7 tons), need find next key fits best value gets closer 10, in case 'moo moo' (weight of 3 tons). so, here function wrote find next fit:

def findnextfit(adict, val, limit=10):                     v=list(adict.values())               # list of dict's values        k=list(adict.keys())                 # list of dict's keys        diff = limit - val                   # value looking         if diff in v:                        # perfect fit.            return k[diff]         elif diff not in v or diff < 0:      # no fit.             return false         else:               # difference positive not perfect fit            vfit = [i in v if < diff]    # list of candidates            return k[max(vfit)]                  # key maximum value 

when test function unexpected result:

>>> findnextfit(cows, 7, 10) 'lola' 

where should have result 'moo moo'.

anybody can point me @ i'm doing wrong in findnextfit function?. have been working on whole day , i'm running out of ideas.

your issue here:

 if diff in v:                        # perfect fit.        return k[diff] 

diff not index @ corresponding key entry value diff be.

you can fix using index* method:

if diff in v:     return k[v.index(diff)] 

similarly in third conditional you'd have do: return k[v.index(max(fit))]

also second conditional should not or rather and.

with fixes:

def findnextfit(adict, val, limit=10):                   v=list(adict.values())               # list of dict's values      k=list(adict.keys())                 # list of dict's keys      diff = limit - val                   # value looking       if diff in v:                        # perfect fit.          return k[v.index(diff)]       elif diff < 0:      # no fit. don't need check if diff not in v because earlier if wouldve caught case          return false       else:               # difference positive not perfect fit          vfit = [i in v if < diff]      # list of candidates          if not vfit: return false          return k[v.index(max(vfit))]                  # key maximum value 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -