← Back to challenges

Are Pairs Sufficient for a Clear Ordering?

PythonHardarrayslogic

Instructions

Create a function that returns True if an array of pairs are sufficient for a clear ordering of all items.

To illustrate:

clear_ordering([["D", "A"], ["C", "B"], ["A", "C"]]) ➞ True
# Since unequivocally: "D" < "A" < "C" < "B"

On the other hand:

clear_ordering([["D", "A"], ["B", "A"], ["C", "D"]]) ➞ False
# Since we know that "C" < "D" < "A", and we know "B" < "A"
# but we don't know anything about "B"s relationship with "C" or "D".

Examples

clear_ordering([["S", "T"], ["T", "U"], ["U", "V"]]) ➞ True

clear_ordering([["T", "S"], ["T", "U"], ["U", "V"], ["V", "W"]]) ➞ False

Notes

See Comments for a good visualization of the question.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.