Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

Hey guys! So here's a tough one for you. I'm working on the last problem set (ps11) and I encountered a problem related to the "in" operator. Details below.

OpenStudy (anonymous):

So I'm trying to create and add nodes to my graph based on the code attached to the problem set. The problem is: nodes that I create and should be treated as equal (those which have the same name - as defined in the Node class) are only treated equal by the "==" operator and not the "in" operator. So the hasNode() method basically does not work. Let me know if you need more clarification or actual code.

OpenStudy (anonymous):

Can you paste the code?

OpenStudy (anonymous):

Hmmm... this quick test seems to work http://dpaste.com/1422496/ Paste your code and we will have a look 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.

OpenStudy (anonymous):

Sorry, guys, false alarm :) It works fine with a list, it only has a problem with a set. But as far as I understand, sets are implemented using hashes so some __hash__ method need to be defined for the Nodes before putting them in sets. I guess that means that you're not supposed to store the Node objects directly in your Digraph, only corresponding strings. Am I correct? Anyway, I made a huge detour to figure that out... :S

OpenStudy (anonymous):

I don't recall all the details of the pset but I did have this in my graph.py and I used it in my map/graph - maybe I went where you went. ``` class SmartNode(Node): def __hash__(self): return int(self.name) ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!