FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Define equality of MCState by ruddyscent · Pull Request #21 · davecom/ClassicComputerScienceProblemsInPython · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Chapter2/missionaries.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def __str__(self) -> str:
"The boat is on the {} bank.")\
.format(self.wm, self.wc, self.em, self.ec, ("west" if self.boat else "east"))

def __eq__(self, other) -> bool:
return (self.wm == other.wm) and (self.wc == other.wc) and \
(self.em == other.em) and (self.ec == other.ec) and \
(self.boat == other.boat)

def __hash__(self) -> int:
state: int = self.wm * (MAX_NUM + 1)**3 + self.wc * (MAX_NUM + 1)**2 + self.em * (MAX_NUM + 1) + self.ec
state *= 1 if self.boat else -1
return hash(state)

def goal_test(self) -> bool:
return self.is_legal and self.em == MAX_NUM and self.ec == MAX_NUM

Expand Down

Back | FazBrowse Home | New Git URL