A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 2 years ago
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()?
anonymous
 2 years ago
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()?

This Question is Open

anonymous
 2 years ago
Best ResponseYou've already chosen the best response.0it will help if we can see your code. the builtin 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
 2 years ago
Best ResponseYou've already chosen the best response.0bwCA  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
 2 years ago
Best ResponseYou've already chosen the best response.0Just 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.

anonymous
 2 years ago
Best ResponseYou've already chosen the best response.0Just 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
 2 years ago
Best ResponseYou've already chosen the best response.0It 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
 2 years ago
Best ResponseYou've already chosen the best response.0what 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] ```
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.