57
Awesome Team
Vedran Čačić
https://web.math.hr/~veky
Last seen 13 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.
Aaargh. My eyes hurt. :-P
First, this is a classic example of how to go overboard with names. It would be _more readable_ if game_result was named just `r`. :-P
And those comments should really be names.
m_diag = r[0][0] + r[1][1] + r[2][2]
Or even
m_diag = ''.join(row[i] for i, row in
More
This is a standard, and I hope intended, solution for this task. However,
it's interesting to know that Python has a tool for specializing functions
directly: functools.partial. So you don't have to define a new function,
you just construct a partial object from sorted on key=abs, and name it
checki
More
A nice idea, but could be written much more directly. Check [this one](https://py.checkio.org/mission/three-words/publications/hrvoje/python-27/slightly-weird-solution/?ordering=most_voted&filtering=all) for example. :-)
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
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
You [win](https://py.checkio.org/mission/most-wanted-letter/publications/veky/python-3/key/?ordering=most_voted&filtering=all) the bet. :-D
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 can make it in one function without problems:
rn = ((1000, 'M'), ...)
def checkio(x, pos=0):
try: num, rom = rn[pos]
except IndexError: return ''
else: return n // num * rom + checkio(n % num, pos + 1)
Also, unpack instead of indexing. Also3, EAFP is powerful. ;
More
You don't need .keys(), and you don't need [] inside ''.join(). And cutone is a horrible name given what it's doing. :-P
More
`while bool(blah)` is exactly the same as `while blah`, only slower. You can also write `while bool(bool(bool(blah)))`. :-D
And that `next(..., False)` pattern is much better expressed using EAFP in line 6, and simple `any` in line 11.
More