veky
on Dec. 21, 2014, 5:59 p.m.
<p>[since CiO will probably never get back links to commenter's solutions :-(, [here is the link to mine](<a href="http://www.checkio.org/mission/mind-switcher/publications/veky/python-3/zendoh/?ordering=most_voted),">http://www.checkio.org/mission/mind-switcher/publications/veky/python-3/zendoh/?ordering=most_voted),</a> for comparison. This is a reply to ignalion's comment on my code.]</p>
<p>While you might think our solutions are very similar, I see many subtle (and some not so subtle) differences. [Obviously they are <em>algorithmically</em> similar, but that's probably because we have both either watched that episode, or read the beautiful Wikipedia article about it.:] Let's analyze them in the hope of learning something. :-)</p>
<ul>
<li>Your solution is swarmed by comments. It always puzzles me why people write code twice: first time in comments, second time in real Python. It makes sense when you're writing some lower level languages, since then those two explanations are on fundamentally different level - one is for your fellow humans who like abstractions, and another is for simple-minded compiler that only cares about the machine code it will produce. But in Python, there really is no need for that.</li>
</ul>
<p>Precisely, of that huge comment#2: first sentence is wrong didactically, technically, contextually and stylistically. It really has no place there. Second one is fine in intention, but really should be much clearer if it talked about <em>cycles</em>, not about "graphs within graphs" (seriously, what?:). Everything else is implementation, and really should be written in a language suited for that: namely, Python.</p>
<p>Comment#1: I think even you'll agree it contributes precisely nothing. [Much better would in fact be to invent a better name for "robots" dict.] Comment#3 is again nice in insight (every phase switches N and S), but the rest is just something that belongs in the code.</p>
<p>And speaking about odd numbers is red herring - the only important thing is whether N and S are swapped or not. This shows another problem with comments: they can easily get out of sync with code. Above you explicitly says you consider "graphs" with 1 node too. But you don't count them below in your "odd number", right? Code says something subtly different than comments. This is a maintenance nightmare: which one do we trust? It can easily be avoided by commenting only things that are too high level even for Python (and keeping in mind that the bar is <em>much</em> higher than in e.g. C), and writing everything else where it belongs: in statements and expressions.</p>
<ul>
<li><p>Your result type is poorly chosen: tuple doesn't really support incremental adding at the end. You see it both syntactically (you have to make 1-tuple for +=, and that's ugly) and semantically (you make new and new tuples all the time, instead of just appending to a list). And algorithmically too: what you have is called "Schlemiel the painter's algorithm" (in not very positive sense:), since the number of copyings and garbage collectings is quadratic, without good reason.</p></li>
<li><p>You have some "interesting" code duplications in your loop. They don't make code much longer, but they make it harder to follow, since the eye has to go up and down, wondering what slight detail is changed this time. And it can be done without duplication, as my code shows.</p></li>
<li><p>Partly as a consequence of duplication, your code has sprinkled side-effects everywhere. .popitem, then two .pops... it's not so bad, but can be reduced. Why not `body, mind = robots.popitem(mind)` too? It would be more consistent, and easier to read.</p></li>
<li><p>A small detail, but important: Python assigns iterating names using (almost) the same procedure as other names - that means unpacking is possible there, too. Lines 4~5 are better written as `for body, mind in journal:`.</p></li>
<li><p>A philosophical point: while you obviously subscribe to mind-body dualism, and track pairs of "minds" and "bodies" as separate entities, I am just concerned with question "where is x's brain" (expressed through `brain` function). Here, of course, it is not about who's right or wrong, but it's interesting how different philosophies of mind naturally produce different solutions. :-)</p></li>
</ul>