8
Guido van Rossum
Last seen 8 years ago
Member for 11 years, 1 month, 16 days
Difficulty Normal
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
y_size = math.floor(total / 2) + 1
Again, I think you can use // and avoid using the math module:
More
return total * len(stones) / 2
See note about constants and big Oh notation below.
More
best = diff
At this point you can actually stop if best == 0:
if best == 0:
return
More
diff = abs(2 * part - total)
Style nit: Contrary to what many people seem to believe PEP 8
specifies, I'd like it when whitespace reflects the binding strength
of the operators:
abs(2*part - total)
More
for s in itertools.chain.from_iterable(itertools.combinations(stones, r) for r in range(1, max_size + 1)):
This way of writing two nested loop as one using
itertools.chain.from_iterable() feels a bit confusing. (I actually
had to look it up!) Since you don't need to break out of the
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 = None
If you initialize best to a large enough value you won't need to
special-case "best is None" below. It turns out 'total' is the
perfect "large enough" value, and it produces the right answer if the
list is empty. (Thinking about extreme cases often helps understand
the
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
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
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
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
'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