27
Last seen 3 months ago
Member for 10 years, 4 months, 9 days
Difficulty Normal
Interesting code, but huge :) That's why I dislike objects.
Look at http://www.checkio.org/mission/snake/publications/blabaster/python-3/autious-approach-2/
And last, but not least:
assert check_solution(checkio, ['...T10.T..',
'.T..2T...T',
More
curr_throw = collections.defaultdict(lambda: 0) # no!
curr_throw = collections.defaultdict(int) # better
curr_throw = collections.Counter() # best
# so on
new_units = collections.defaultdict(lambda: 0.0) # no!
new_units = collections.defaultdict(float)
d =
More
if u1 < 1 or prob < 1/1e6: # ignore such small chances
Heh, hard way.
This task can be resolved by max(units_1, units_2) steps (or less).
Look at http://www.checkio.org/mission/battle-dice/publications/blabaster/python-3/first/, line 21, it shows how to cut idle forks.
More
s_col = [sum(r) for r in zip(*matrix[:])]
zip() doesn't damage args, so "[:]" redundant.
More
if len(letter) != 0: # no!
if not letter: # empty iterable (ie list, set, string)
# returns False (if checked as bool)
More
Bubble seems a good idea, but really not: the same number of comparisons (as most common algo) plus useless swaps.
A bit about codestyle:
list2=[]
for i in sequence:
list2.append(i)
# shorter:
list2 = list(sequence)
...
for i in r
More
if len(data) % 2 is 0:
No!
First, the results of this "is" comparison with with "calculated objects" are undefined (depends on compiler).
On Cpython it works, because it keeps **some small integers** in pull, but this is a derty hack.
The only legal and recommended "reserved" argument for "is"
More
1. Why recursion? Strange :)
2. "floor", "ceil" - names from math namespace. "lo" and "hi" looks more reasonable.
More
What about divmod:
for integer, numeral in MAP:
count, n = divmod(n, integer)
r.append(numeral * count)
return ''.join(r)
More
... functions and method calls are expensive (more so than in C or Java) ... /by Guido/
More
gun = tuple(
tuple(map(int, s)) for s in
('000000000000000000000000100000000000',
'000000000000000000000010100000000000',
'000000000000110000001100000000000011',
'000000000001000100001100000000000011',
'110000000010000010001100000000000000',
'11000000001000101100
More
What about style?
lines 15..16 - chain assignment:
p = r = 0
lines 17, 19, 39 - redundant brackets:
while p < lm:
lines 23, 24, 25, 26, 43, 45 - increment:
p += tmp
And, of course:
def f(r, c):
return (r + c) % 2 + 1
One more advice: http://pep8online.com/
More
Every python's object returns True or False, if you ask ;)
For numbers - zero returns False, for list or dict (etc) empty returns False
ie
x = 777
if x: # True
pass
More
((1,2),(2,1))[row%2][col%2]
Too heavy for my mind
(rown + coln) % 2 + 1
:=))
More
# line №5:
grid = list(map(list, crossword))
# lines №№133..138:
return tuple(map(''.join, grid))
# It's a python ;)
More
... explicit is better than implicit ...
It's a Hell:
self.connections = set()
for con in connections:
self.add(con)
How to relate self and self.connections?
I urge python's guru.
More
elif i == "POP" and len(A): sum+=A.pop()
elif len(A): sum+= A[-1]
Add a bit of elegance:
else:
if A:
sum += A.pop() if i == "POP" else A[-1]
and name var as a "sum" is a bad practice :(
More