3 ms·
Here's one possible implementation of valueOf: import numpy as np def valueOf(*v): return sum( v * np.power(10,range(len(v)))[::-1] ) > valueOf( 2,
by beambot 12y ago
Here's one possible implementation of valueOf:
import numpy as np
def valueOf(*v):
return sum( v * np.power(10,range(len(v)))[::-1] )
> valueOf( 2,3,4 ) # => 234
Also, in python 2.7, I don't think you can put a named variable proceeding a wildcard? At least, that doesn't work for me, so I left it out.
- TheLoneWolfling 12y agoThat is a) hauling in an awfully big external library for something that should be relatively trivial, b) inefficient (you're replacing a single multiplication by 10 and addition per digit with O(log x) multiplications and an addition per digit, assuming np.power uses a decent exponentiation algorithm), and c) if anything, less readable than my version. To put it simply, I don't see an improvement with that version. And Python 3 has some improvements w.r.t. wildcard arguments.
- bpicolo 12y agoIf you don't need other bases you can just string join them. : P
- TheLoneWolfling 11y agoThat's actually how I started, and `int` has a base parameter. But it's still relatively verbose, due to Python's whole "join-only-takes-strings" thing: def valueOf(*values, base=10): return int("".join(map(str, values)), base=10) Not to mention it's doing a whole lot of unnecessary work.
- TheLoneWolfling 11y agoWhoops. Miscopied. the second `=10` shouldn't be there.