Skip to content

Conversation

@SilverSolver
Copy link
Collaborator

Initial solution and some tests!

@@ -0,0 +1,49 @@
from collections import defaultdict

def dfs(ordered_vertices, edges, color, v):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dfs is seriously scary.

I think it needs at least some comments. What does it return and why?

My current guess is topsort order and a color array if it exists or None if the graph has a cycle, but why return them if the function modifies them anyway?

Also imho searching for cycles and topsorting in the same function causes unnecessary confusion. Maybe comments would help, but why not split this stuff into two functions anyway?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think we should look for cycles during the topsort, because they both require searching on graph. I liked the coloring, which helps detecting if it is possible or not, but not sure how to use it for finding maximal satisfying subset.

Also, comments would be great indeed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll add comments soon

N = len(symbols)
color = dict()
for v in symbols:
color[v] = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stuff like this could look cleaner as
color = dict.fromkeys(symbols, 0)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree

for w1, w2 in zip(dictionary[:-1], dictionary[1:]):
for i in range(min(len(w1), len(w2))):
symbols.add(w1[i])
if w1[i] != w2[i]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, I do get the feeling that there's a lot going on in these functions.

Possibly splitting up some of the logic could help?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments here. Hope it will be more clear now.

ordered_vertices.extend(residual_symbols)
return ordered_vertices[::-1]

def unknown_alphabet(dictionary):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of the function is not definitive. Something like "find_alphabet" might be better?

self.assertFalse(unknown_alphabet(["1", "2", "3", "2"]))

def test_2(self):
self.assertEqual(unknown_alphabet(["1a", "1b", "a1", "ab"]), ["1", "a", "b"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests, we should merge them with @crossopt 's unittests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've already think about it

@@ -0,0 +1,49 @@
from collections import defaultdict

def dfs(ordered_vertices, edges, color, v):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think we should look for cycles during the topsort, because they both require searching on graph. I liked the coloring, which helps detecting if it is possible or not, but not sure how to use it for finding maximal satisfying subset.

Also, comments would be great indeed.

@SilverSolver SilverSolver changed the title First version of solution (see "unknown_alphabet" function) Second version of solution (see "unknown_alphabet" function) Jul 17, 2018
@SilverSolver SilverSolver changed the title Second version of solution (see "unknown_alphabet" function) Solution with comments (see "unknown_alphabet" function) Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants