[sf-lug] [PYCLASS] Still on exercise 2.1

Jeffrey Malone ieatlint at tehinterweb.com
Sat Oct 4 01:02:39 PDT 2008

Just a couple ideas...

Firstly, to point out two things that may be of interest with the 2.4 exercise:
pi = 3.14159
print ((4/3.0)*pi)*(5**3)

This is not incorrect.  But I figured I'd add that chapter three
explains why two things could be changed.
First, python includes a pi constant in the math library, so you don't
need to define it.  And second, the order of operations in python
follow what a scientific calculator produces, such that parenthesis
are useless in this expression:
import math
print 4/3.0*math.pi*5**3

With the second problem, Jim's answer is indeed correct, but the
conversion of time to a decimal format can be more cumbersome than
it's worth.

/* Set some constants */
MIN = 60
easy_pace = 8*MIN+15
tempo_pace = 7*MIN+12
start_time = 6*HOUR+52*MIN

secs = easy_pace * 1 + tempo_pace * 3 + easy_pace * 1
print "%d:%02d" % ( (secs/ MIN), (secs%MIN) )

/* Now add start time plus run time and convert to hours, minutes and seconds */
total = start_time + secs
print "%d:%02d:%02d" % ( (total/HOUR), (total%HOUR/MIN), (total%HOUR%MIN) )

The reason why this can be considered "more correct" is that time is
always represented in seconds in UNIX systems (eg, the well known UNIX
epoch).  Keeping to that format allows you to access time handling
functions, if you so desire.  Normally if you were playing with time
so much, you'd fall back onto the time module, which handles the math
for you (and greatly expands your capabilities).  If anyone wants to
see an example on how the time module can "simplify" this, let me

This may look more confusing, and perhaps it is.  But when you know
you're always dealing with seconds, it's a lot easier to convert
between values, and there's no chance of anything being truncated
(technically speaking, it should be slightly faster too, as floating
point math is slow -- but you're talking milliseconds).

Also, Jim forgot to put down the final time as 7:30:06 ;)

On Fri, Oct 3, 2008 at 11:50 PM, Rick Moen <rick at linuxmafia.com> wrote:
> Quoting jim (jim at well.com):
>>    re-express all time in decimal format.
> It's taken 211 years for it to catch on, but, folks, _you_ were here,
> at the glorious moment of <<Duodi 12 Vendémiaire, an CCXVII a 9h78m81s>>,
> when French Revolutionary Time finally caught on!
> Vive la France!  Vive le réolution!  A bas les aristos!
> http://en.wikipedia.org/wiki/Decimal_time#France
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug

More information about the sf-lug mailing list