8
Guido van Rossum
Last seen 8 years ago
Member for 11 years, 19 days
Difficulty Normal
This is a perfectly acceptable solution for passwords, which are
typically not very long. I am going to give you some advice on how to
make the function faster, but realize that in practice you won't be
able to measure the speedup, unless you are calling this function (and
nothing else) millions
More
def checkio(labyrinth):
# I'm definetely a fan of Dijkstra's (!!!)
Funny, Dijkstra is a person, but you seem to be using his name exclusively
to refer the algorithm he published. :-)
# Object oriented Dijkstra: each cell contains 3 fields
# (and I will
More
Nice one! Not a word too many. Still, I couldn't help thinking of what may be an improvement: check for a cycle before putting the path into the queue. It would be something like
for next in all_node:
if next not in path and {path[-1], next} in network:
q.append(path + (next
More
Cool solution! Very clever use of str.split() and a strict interpretation of the input format description (it never actually says that there's exactly one space between each part of the error argument :-). Agreed with all of veki's comments, but those are really just nit-picks, and the code is jus
More
It's interesting that you prefer making several passes over the input
string just to avoid splitting it in case you have a horizontal match.
In my own solution I first broke the text up in lines and then
searched each line for the word. In the extreme, your approach will
find the horizontal matc
More
This solution is awesome! I just have one (software engineering) suggestion -- find a better name for the function L(). Maybe just collinear()? (Note that the word has two ells.) Then you can rename the variable just 'row'. Oh, and the arguments should better be called p, q, r instead of x, y, z --
More
Nice use of defaultdict(set), and I like the optimization of subtracting the best trace from all_nodes. Also, you used the logic I recommended to Sim0000 for putting the cycle detection at the end. But you lose half a point for using reduce(). :-)
More
return 1 << (len(stones) - 1)
This is probably better written as 2 ** (len(stones) - 1), so the
reader doesn't have to know about the obscure left-shift operator and
its relationship to powers of two.
More
'Return True if password strong and False if not'
Most programming style guides use "triple double" quotes for docstrings, e.g.
"""Return True if the password is strong and False if not."""
I also recommend using full grammatical sentences and ending with a period.
More
if __name__ == '__main__':
It's hardly necessary to add the 'First' or 'Second' messages to the
assert statements, as it prints the failed line in the traceback, so
you can see exactly which line went wrong already. Adding a message to
assert is useful (a) when there is a framework that supp
More
I don't think this solution is particularly clear -- I like your projective math version better, and nickie's simplified version even better (as a puzzle spike).
Some software engineering / readability comments (which is mostly my angle these days):
"len2" is an odd name -- I'd call it distanc
More
I had a hard time deciding on the vote. On the plus side, this is a fairly
complex algorithm and the implementation is correct as far as I can verify.
It also illustrates the A* algorithm nicely. On the minus side, the code is
riddled with poor style (pep8.py gave me *44* error message for this file
More
def min_diff(self, stones):
What should the answer be if stones is an empty list? I think it
should be zero, since the difference between two empty lists is zero.
This fits nicely with the difference computed when it is a list with
only one item -- then the answer will be equal to that
More
y_size = math.floor(total / 2) + 1
Again, I think you can use // and avoid using the math module:
More
max_size = math.ceil(len(stones) / 2)
Use the // operator ("floor division") to do integer division with
truncation. The rounding up can be done by adding one first:
max_size = (len(stones) + 1) // 2
Although thinking a little more about the algorithm, I think it's oka
More
best = diff
At this point you can actually stop if best == 0:
if best == 0:
return
More
best = math.floor(total / 2)
You can use // again here. You can then remove "import math" from
your code -- I always get a little sad seeing the (floating-point)
math module imported by code that fundamentally deals with integers.
More
return x + x_size * y
It's clear that you really want a two-dimensional list; then you could
just write table[x][y] instead of table[index(x, y)]. I suppse you've
been badly bitten by the bug in this piece of code for creating such a
table:
# DO NOT USE THIS
More
return approach.min_diff(stones)
For extra points, you could run both solutions in parallel threads and
stop whenever the first one is done.
More
approach = min(ALGORITHMS, key=lambda algorithm: algorithm.time(stones))
This is cute, but you don't know if the time functions give calibrated
results -- the constant factors could be quite different for the two
algorithms. I guess it doesn't matter too much, as long as it selects
the
More