The problem
Each fact says that two names are equal ("a==b") or not equal ("a!=b"). Equal is what you expect: if a equals b and b equals c, then a equals c. Return True if all the facts can hold at the same time, and False if they contradict each other.
consistent(["a==b", "b==c", "a!=c"]) # False
consistent(["a==b", "c!=a", "c==d"]) # True
consistent(["x!=x"]) # False
Names are made of the letters a to z and can be longer than one letter, such as "delhi==dilli". There are no spaces.
Some tests join long chains. Write find with a loop, or make sure your trees stay short (lessons 2 and 3): a recursive find on a path of 100,000 nodes stops with RecursionError.
Limits
- Up to 200,000 facts and about 200,000 different names.
Builds on DSUF-5 Applications.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | Can all hold | Not run | |
| 3 | Order of the facts | Not run | |
| 4 | Not equal to itself | Not run | |
| 5 | Equal to itself | Not run | |
| 6 | Longer names | Not run | |
| 7 | No facts | Not run | |
| 8 | A long chain, all fine | Not run | |
| 9 | A long chain, one clash | Not run |