Skip to content

Commit 3bc898f

Browse files
committed
test(algorithms, graphs, topological-sort): utility test to validate ordering
1 parent c1630a1 commit 3bc898f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

algorithms/graphs/alien_dictionary/test_alien_dictionary.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def test_alien_order_2(self, words: List[str], expected: str):
5858
actual = alien_order_2(words)
5959
self.assertEqual(sorted(expected), sorted(actual))
6060

61+
def is_valid_alien_order(self, words: List[str], order: str) -> bool:
62+
if not order:
63+
return False # or handle expected empty case separately
64+
char_rank = {c: i for i, c in enumerate(order)}
65+
for w1, w2 in zip(words, words[1:]):
66+
for c1, c2 in zip(w1, w2):
67+
if c1 != c2:
68+
if char_rank.get(c1, -1) > char_rank.get(c2, -1):
69+
return False
70+
break
71+
else:
72+
if len(w1) > len(w2):
73+
return False
74+
return True
6175

6276
if __name__ == "__main__":
6377
unittest.main()

0 commit comments

Comments
 (0)