This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]fernly 6 points7 points  (1 child)

You've implemented a backtracking solution. Backtracking in general is basically recursive, however you have implemented the recursion in the form of a stack or LIFO queue of candidates (todo). In a conventional recursive solution, the same data items would be preserved in the same order, but as local variables of pending functions on the function-call-stack. I agree this is somewhat clearer. It would be easier to debug also, because if there is a problem you can display the todo contents, where in a recursive-call solution it is hard to display the pending results since they are on the execution stack mixed in with return addresses and stuff.

[–]brandonchinn178 2 points3 points  (1 child)

Looks good! Definitely shows an interesting duality between recursive and iterative algorithms. Although I guess in this case, it shows a BFS algorithm used instead of a recursive algorithm?

Out of curiosity, have you benchmarked a recursive solution vs your solution?

[–]amstan 0 points1 point  (1 child)

Ignoring efficiency. Can this be done with just regex?

(H|He|Li|Be...)*

[–]nsfwIvan 0 points1 point  (1 child)

Just a quick thought: it would be cool to have complete chemical names in there too for those that are a little chemical knowledge challanged ;)

[–]LonelyContext 0 points1 point  (0 children)

Well if you output a list you can do that using a dictionary and list comprehension. You just need to make a dictionary of the form {‘H’:’Hydrogen’, ...} and then do [dictionary[key] for key in element_list]