57
Awesome Team
Vedran Čačić
https://web.math.hr/~veky
Last seen 15 hours ago
Member for 11 years, 6 months, 6 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.
Don't use int for truncation, it's bad style (Guido tries to deprecate such usage). When floordividing, use the dedicated operator: `n // 2`.
Even better: since you need both // and %, why not acquire both at once:
half, odd = divmod(len(data), 2)
if odd:
return sum(data[half-1:hal
More
This doesn't work like builtin min/max.
>>> min(3)
TypeError: 'int' object is not iterable
>>> from checkio.min_max.smirnoffs.first import min
>>> min(3)
3
I'm not saying your way makes less sense. :-)
More
While you're importing, you can
from operator import mul
and use that as the first argument to reduce. :-)
Also, if you're using reduce, you can go old school all the way and use map and filter too, instead of that ultramodern list comprehension. :-D
More
`any(... for ...)` is better than `any([... for ...])`. Among other things, it shortcircuits the evaluation as soon as it finds a true value. List comprehension always constructs the whole list.
More
This solution is not good. Besides "special cases are not special enough",
and not using modern Python idioms like sorted(), it has a math error.
While c1d and b1d are calculated correctly, a1d _doesn't_ have to be 180 -
their sum. See http://goo.gl/UMrZNd and the resulting discussion.
More
Here is your solution in True Python:
def checkio(data):
res, vals = [], [(1000, 'M', 2), (500, 'D', 2), (100, 'C', 4),
(50, 'L', 4), (10, 'X', 6), (5, 'V', 6), (1, 'I', None)]
for val, rom, next in vals:
res += data // val * rom
data %= v
More
You're writing Python3, use it. :-) / works correctly, you don't need float(). And In line 3, you can use // directly instead of casting to int.
Even better, since you need both // and % of same operands, it's better to calculate both at once:
n, nudge = divmod(len(data)-1, 2)
return (data
More
Discussion [here](https://checkio.org/mission/non-unique-elements/publications/veky/python-3/two-bins/#comment-44259).
More
You [win](https://py.checkio.org/mission/most-wanted-letter/publications/veky/python-3/key/?ordering=most_voted&filtering=all) the bet. :-D
More
Suggestions? How about [this](https://checkio.org/mission/most-wanted-letter/publications/veky/python-3/key/?ordering=most_voted)? :-)
But generally... if you want to map letters to their counts, a dict is much better structure than a list. You can use collections.OrderedDict if you really need to
More
Don't iterate over indices when you can iterate over the list itself.
for element in array[0:len(array):2]:
sum += element
BTW do you really need elif? Can len(array) be less than zero? :-) And while we're at it, a is already 0 in the empty case, you don't need to set it again. :-]
More
Nice. Reminds me of definition by cases. :-) Of course, in LaTeX you would have `\\` instead of single `\`. :-D
More
`for spam in map(xform, eggs)` is almost always an antipattern. You're packaging one iteration into an expression, just to evolve it in a suite right afterwards. Since you already have a suite, `map` gives you no ROI. :-)
for word in words.split():
if word.isalpha():
...
More
List comprehensions are cool, but generator expressions are moar cool. :-) Just drop the brackets:
' '.join(v for k, v in ... if not number % k)
The difference is that you don't construct a list of all the values at once, traversing it again when joining, but you construct values one by one, j
More
You don't need lines 6 and 7. Even if you did need them, raising a more specific thing like RuntimeError or even better ValueError, or even betterer OverflowError, would be more useful.
Also, you can delete parentheses if you exploit the fact that multiplication is commutative. :-)
converted +
More
`str.maketrans` would really help here. :-)
And `t,h,d,m = map(int, str(n).zfill(4))`, of course.
More
You really like that `True if condition else False` idiom? `condition` does the same thing.
Also, see `any` builtin.
More