On Wed, May 26, 2010 at 7:21 PM, Andre Roberge <[email protected]> wrote:
> > Ok, here's my solution: > >>>> def make_root(i): > ... def _root(x): > ... return pow(x, 1.0/i) > ... return _root > ... >>>> for i in range(1, 11): > ... locals()['root' + str(i)] = make_root(i) > ... >>>> root2(4) > 2.0 >>>> root4(64) > 2.8284271247461903 >>>> root6(64) > 2.0 > > Cheers, > André > Much appreciated André! I think our solutions are similar: """ http://mail.python.org/pipermail/edu-sig/2010-May/009998.html http://mail.python.org/pipermail/edu-sig/2010-May/009999.html """ def makeroot(N): try: assert type(N)==type(1) and N>=0 except: raise ValueError("0 <= N <= integer") fname = "root" + str(N) if N==0: fdef = "globals()['%s'] = lambda x: pow(x, 0)" % fname else: fdef = "globals()['%s'] = lambda x: pow(x, float(1)/%s)" % (fname,N) exec(fdef) Kirby _______________________________________________ Edu-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/edu-sig
