57
Awesome Team
Vedran Čačić
https://web.math.hr/~veky
Last seen 23 hours ago
Member for 11 years, 6 months, 7 days
Difficulty Advanced
We shall not cease from exploration, and the end of all our exploring will be to arrive where we started and know the place for the first time.
You don't need () after keywords (if, elif...).
Also, you can use `not a%b` to check if a is divisible by b (you don't need parentheses).
Alternatively, you can chain comparisons in line 9:
if number % 3 == number % 5 == 0:
More
Nice and obvious. :-) You could have said
for w2 in words_set - {w1}:
in line 3, it would make your condition simpler.
But check `itertools.permutations`... it is a nice tool made for tasks like this. ;-)
A nitpick: why do you check `w2.endswith(w1)` instead of other way? Of course it's equi
More
hash? Ah, I should have known it. You came here from the Kingdom of Perl. Noone else would check for substring using regex. :-D
That being said, study collections.Counter and don't invent the wheel all over again. :-)
Line 14: m = max(count_dict, key=count_dict.get)
keys.sort(), then return keys[
More
ROT13 effect in line 5 is really nice artefact. ;-D
But I'm confused... of five lines, you spend one just to introduce a semantically redundant name for the next line (without it, it still wouldn't be longer than line 5, so you weren't concerned of the right margin). Probably the real reason was so
More
[] is probably nicer way to write list(). But this is ok if you really want to be explicit. :-) And if you wrote [], you'd probably be more inclined to write it as a list comprehension, though explicit loop is also fine.
More
Yeah, at first I thought inheriting from Counter would be a nice idea. However, it's insistance on reducing negative counts to 0 is painful to fight against.
Instead of that ugly polynomialize, you could have written something much more elegant: eval accepts a locals() dict as a second argument. So
More
BASE.find(str_number[i]) really could be factored out.
Instead of that ugly index manipulations, you can use enumerate (and possibly reversed).
More
Don't write empty except. It almost never means what you think it means.
For example, imagine someone confuses order of arguments, and calls index_power(3, [1,2,3,4]). You'd want TypeError, right? But you get -1. :-(
More
Since you have two lines anyway, how about extracting definition of l to separate line? :-)
Also, you shouldn't use mutable defaults unless you _really_ know what you're doing. ps=() is much better. (Usually, = in default definitions doesn't have spaces around it. At least PEP8 says so.:)
More
Names, names, names! :-) And output is obviously unnecessary, just concatenate them.
def construct_numerals(number, pos):
quot, rem = divmod(number, 5)
ones, fives, tens = NUMERALS[pos]
if rem == 4:
return ones + (tens if quot else fives)
else:
More
Nice idea, but really too complicated implementation. See:
checkio=lambda t:3*"True"in"".join(str(w.isalpha())for w in t.split())
More
`set(blah.split(","))` could have been a separate function, though duplication isn't huge here.
But making something a list just so you could sort it and use the result once is an antipattern. That's what sorted is for.
",".join(sorted(a & b))
More
Cool idea, can be onelined without much problems.
checkio=c=lambda s,w=1:len(s)and max([s[0]+c(s[2:],0),s[0]+c(s[1:])]+w*[c(s[1:],0)])
Especially jarring is "whisker == True" instead of just whisker. Comparing bools to True makes as much sense as adding ints to 0. :-P
More
Wouldn't it be nicer if lambdas were declared with real number of parameters, and called with *args? If you insist on indexing, "lambda x:" really should be factored out.
More
You can use genexps in a much more versatile way:
count_neighbors = lambda grid, row, col: sum(grid[i][j]
for i in range(row-1, row+2) if i in range(len(grid))
for j in range(col-1, col+2) if j in range(len(grid[i]))
if (i, j) != (row, col))
(Also, ranges are cool. But
More
You could have made even better:
checkio = {'0'*6, '707409', '100478', ...}.__contains__
;-)
More
Of course you can just delegate to int and inband its ValueError as -1. But let's ignore that. :-)
`abc` is a lousy name. `digits` would be much more appropriate, considering what they represent.
Using `enumerate` is laudable, but you're doing it wrong. Manual tweaking of index makes it pointless.
More