3 ms·
from functools import reduce def valueOf(*values, base=10): return reduce(lambda x,y: x*base+y, values)
by im3w1l 12y ago
from functools import reduce
def valueOf(*values, base=10):
return reduce(lambda x,y: x*base+y, values)
- TheLoneWolfling 12y agoDoesn't work with a zero-length input (although that's easily fixed). That being said, that's much cleaner. Thanks!
- im3w1l 12y agoSure. But it isn't clear to me it should work with zero length input. Not working is consistent with the builtin int, int("") throws an exception.
- TheLoneWolfling 12y agoIt's straight from the definition of an integer. An integer is defined as sum(i=0 to n) d_n * base^n. A number with zero digits is just the empty sum, which is canonically defined to be zero. I'll also note that int() returns 0.