-
Notifications
You must be signed in to change notification settings - Fork 16
Amy W #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Amy W #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,47 @@ | ||
| require "set" | ||
|
|
||
| def possible_bipartition(dislikes) | ||
| raise NotImplementedError, "possible_bipartition isn't implemented yet" | ||
| if dislikes.empty? | ||
| return true | ||
| end | ||
|
|
||
| dogz = Set.new() | ||
| doggos = Set.new() | ||
|
|
||
| i = 0 | ||
| return bipartition_helper(i, dislikes, dogz, doggos) | ||
| end | ||
|
|
||
| def bipartition_helper(i, dislikes, dogz, doggos) | ||
| while i < dislikes.length | ||
| dislike = dislikes[i] | ||
| if !dogz.include?(i) && !doggos.include?(i) | ||
| dogz1 = dogz.clone() | ||
| doggos1 = doggos.clone() | ||
|
|
||
| dogz1.add(i) | ||
| doggos.add(i) | ||
|
|
||
| return bipartition_helper(i, dislikes, dogz1, doggos1) || bipartition_helper(i, dislikes, dogz, doggos) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally seeing this pair of recursive calls in this method set off alarm bells, but then I traced through and it doesn't actually make many more recursive calls, it's just a little different from my solution. It does however mean you have do the nested if-else blocks below. I instead assigned the current "color" for the current node and assigned it's neighbors the alternating color, and then continue in a depth-first-search, and I repeat until the stack is empty. If they already have an incompatible color I return false. This results in a bit simpler code. This does work however and work well. |
||
| elsif dogz.include?(i) | ||
| dislike.each { |dog| | ||
| if dogz.include?(dog) | ||
| return false | ||
| else | ||
| doggos.add(dog) | ||
| end | ||
| } | ||
| elsif doggos.include?(i) | ||
| dislike.each { |dog| | ||
| if doggos.include?(dog) | ||
| return false | ||
| else | ||
| dogz.add(dog) | ||
| end | ||
| } | ||
| end | ||
|
|
||
| i += 1 | ||
| end | ||
| return true | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of a
Set.