anonymous
  • anonymous
Hi everyone, I am trying to solve ps 9, but can't figure it out. Two problems: 1. provided solution is not for the ps 9, as described in the .pdf and .py files, 2. I have problems with using cmp functions to order the dictionary. According to specs cmp functions should return boolean after comparing two values, while in python docs I can see that cmp function should return -1, 0, 1. Given that cmp function is (e.q.) cmpWork(subInfo1, subInfo2) how can I use it in ordered()?
MIT 6.00 Intro Computer Science (OCW)
jamiebookeater
  • jamiebookeater
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • anonymous
it will help if we can see your code. the built-in cmp() function has nothing to do with the comparator functions the pset is asking you to make. "Each comparator takes in two (value, work) tuples and returns True if the first tuple is better than the second tuple, and False otherwise. Each comparator has its own definition of “better.” Be sure to read and satisfy the specifications of each comparator." ``` def compareV(t1, t2): if t1[v] > t2[v]: return True return False ``` please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.
anonymous
  • anonymous
bwCA - thank you for your reply. I guess I asn't clear - I used cmp as an abbreviation of compare - custom compare function to be used in ordered(). My code of compare function: http://dpaste.com/1400404/ I have read a lot about how to sort dictionaries both by key and/or value, but cannot understand how to use custom compare function as 'key' function.
e.mccormick
  • e.mccormick
Just a little note about things like: ``` def compareV(t1, t2): if t1[v] > t2[v]: return True return False ``` Boolean operators are used in if because they produce true or false. Therefore, this if is actually not needed because it is returning the evaluation of the Boolean operator. ``` def compareV(t1, t2): return t1[v] > t2[v] ``` That does the same thing.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Just to move forward a bit: given a code: def cmpWork(subInfo1, subInfo2): return subInfo1[1] < subInfo2[1] How should I implement it? sorted(dict.items(), key=cmpWork) this gives an error - missing cmpWork arguments... I know it is silly, but still can't find the answer
e.mccormick
  • e.mccormick
It is defined with arguments. Therefore, it must be called with arguments. ``` def cmpWork(subInfo1, subInfo2): return subInfo1[1] < subInfo2[1] sorted(dict.items(), key=cmpWork(something,something)) ```
anonymous
  • anonymous
what does dict.items() return? you either have to modify your conditional statement to accommodate what you are passing it or you have to modify your key function. maybe try this first; ``` def cmpWork(subInfo1, subInfo2): print locals() return subInfo1[1] < subInfo2[1] ```

Looking for something else?

Not the answer you are looking for? Search for more explanations.