[Tutor] Rounding to n significant digits?
Gregor Lingl
glingl at aon.at
Sun Jul 4 13:43:57 EDT 2004
Alan Gauld schrieb:
>Hi Gregor,
>
>
>
>>def eng_round_to_n(x,n):
>> man,exp = round_to_n(x, n).split("e") # contains error-handling
>> sh=int(exp)%3
>> return "%.*fe%+04i" % (n-1-sh,float(man)*10**sh,int(exp)-sh)
>>
>>
>
>Eek! Are you sure that last line is Python and not Perl?
>Winner of this months obfuscated tutor code I think...
>
>
Hi Alan!
What a nice reply! You are certainly right ... nevertheless I've been
laughing
about it for at least 5 minutes . (Although: it's only the 4th of July
today, who knows
what will follow ...)
Of course I'd be interested in a more elegant solution ...
Unfortunately I cannot work on it right now, because it's only
one hour to the beginning of the finale of then European
football chamionship. I'm going to visit friends to watch it
in television ...
Last week at the Linuxtag in Germany I participated (by accident) in a
so called "coding contest". (Of course I didn't win!) We had to write a
program to solve a certain problem with the sourcecode as
short as possible (!? - strange task!).
Later after some correspondence about it we arrived at:
import sys
for i in range(3):
r=''
for c in sys.argv[1]:
n=int('2l7y202nn6lx6xh0ve',36)//8**(3*int(c)+i)%8;r+=' |_ _ _|
|_|'[2*n:2*n+3]
print r
You may guess what it does. Input has to be an arbitrary sequence of
digits from 0 to 9,
( a so called integer) eg.: 1237583490 ...
... or try it out.
The winning program had less than 140 bytes, as far as I know it was in
Ruby.(Didn't see it)
Regards, Gregor
P.S.: Which code will win the obfuscated code prize now?
More information about the Tutor
mailing list