[Tutor] Tutor Digest, Vol 118, Issue 53
Keith Winston
keithwins at gmail.com
Thu Dec 12 08:00:09 CET 2013
Hey Denis, that was a nice explanation of recursion, thanks.
On Wed, Dec 11, 2013 at 8:37 AM, <tutor-request at python.org> wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
> 1. Re: formatting datetime.timedelta to "HH:MM:SS" (David Robinow)
> 2. Re: recursive function example (spir)
> 3. Re: formatting datetime.timedelta to "HH:MM:SS" (Jignesh Sutar)
> 4. Re: recursive function example (spir)
> 5. Re: formatting datetime.timedelta to "HH:MM:SS" (Mark Lawrence)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 11 Dec 2013 07:43:18 -0500
> From: David Robinow <drobinow at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
> Message-ID:
> <CAF3XjbzRQ5+drp86=
> scv9pNUqtgeT+qKBgJ25zrOuoCaNH7VYQ at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Wed, Dec 11, 2013 at 5:55 AM, Jignesh Sutar <jsutar at gmail.com> wrote:
> > Hi,
> >
> > I've googled around extensively to try figure this out assuming it
> should be
> > straight forward (and it probably is) but I'm clearly missing something.
> >
> > I'm trying to get the total run time of the program but have the final
> time
> > being displayed in a particular format. I.e. without the seconds in
> > milliseconds decimal points and just to customize it a bit more.
> >
> > import time
> > from datetime import datetime
> > startTime = datetime.now()
> > time.sleep(5.1564651443644)
> > endTime = datetime.now()
> > exe_time = endTime-startTime
> >
> > print type(startTime)
> > print type(endTime)
> > print type(exe_time)
> >
> > print "startTime: ", startTime
> > print "endTime:", endTime
> > print "exe_time: ", exe_time #how to format this to "D Days, HH: MM: SS"
> ?
> > #exe_time: 0:00:05.156000
> > #desired 0 Days, 0h: 00:m: 05s
> >
> print str(exe_time).split('.')[0]
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 11 Dec 2013 14:10:26 +0100
> From: spir <denis.spir at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] recursive function example
> Message-ID: <52A86442.4030200 at gmail.com>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> On 12/10/2013 03:48 PM, ugajin at talktalk.net wrote:
> > [...]
>
> Recursivity is hard to get really, meaning intuitively with your guts
> so-to-say.
> Maybe using another example may help. Lets us say you want a function that
> sums
> numbers from 1 up to n, the only input variable. (The result should thus be
> n(n+1)/2.)
>
> def sum_up_to (n):
> # 'res' for result because 'sum' is a Python builtin symbol
> res = 0
> for i in range(1, n+1):
> res += i
> return res
> print(sum_up_to(9))
>
> Now, how to make this recursive? There are multiple ways, all based on:
> # intuitive def:
> sum_up_to(n) = 1 + 2 + ... + n
> # recurrence def:
> sum_up_to(n) = sum_up_to(n-1) + n
> sum_up_to(1) = 1
> We want to just reproduce this second def stupidly in code:
>
> def sum_up_to_rec1 (n):
> if n == 1:
> return 1
> else:
> return n + sum_up_to_rec1(n-1)
> print(sum_up_to_rec1(9))
>
> This does not help much and understand the recursion procedure yet, si let
> use
> add some debug output rightly placed:
>
> def sum_up_to_rec1 (n):
> print("start of func ; n : %d" % n, end=" | ")
> if n == 1:
> return 1
> else:
> sub_res = sum_up_to_rec1(n-1)
> print("n : %d ; sub_res : %d" % (n, sub_res))
> return n + sub_res
> print(sum_up_to_rec1(9))
>
> Run it.
>
> The surprising point, maybe, is that all "start of func..." outputs get
> written
> in a row with all "sub_res..." outputs coming on lines one under the
> other. This
> is the core of the recurrent logic: starting from 9, we call the func for
> 8;
> nothing is yet computed, no result (thus nothing to print as sub_res).
> From 8,
> we call the func for 7; nothing is yet computed, still. Etc... until 1,
> our stop
> value, which finally returns a result. This result for 1 permits the call
> for 2
> to (write the sub_res for 1 *at the end of the big row* and) complete,
> which
> permits the call for 3 to (write the sub_res for 2 and) complete... etc
> until 9.
> The call for 9 is out initial run, when it returns with its product, this
> is the
> final result.
>
> Thus, the logic is such: the execution cascade falls here top-down; but the
> computation cascade inverts gravity and climbs down-up.
>
> Does this help?
>
> Now, why do we execute top-down? After all, the definition by recurrence
> allows
> us very well to start from 1 and go up to n, it would work as well, so why
> not?
> The reason is that we would need to keep n aside, since in this case it
> becomes
> our stop value; but we still need some index, here going up from 1 to n.
> Thus,
> we need another parameter to the recurrent func, say like in the easy func
> above. And in fact we need yet a third parameter, namely the partial
> result: how
> else would the next call know the partial result? This gives something
> like:
>
> def sum_up_to_rec2 (n, i=1, sub_res=0):
> # 'res' for result because 'sum' is a Python builtin symbol
> print("start of func ; i : %d ; sub_res : %d" % (i, sub_res), end=" |
> ")
> if i == n:
> return i + sub_res
> else:
> sub_res += i
> print("sub_call ; sub_res : %d" % sub_res)
> return sum_up_to_rec2(n, i+1, sub_res)
> print(sum_up_to_rec2(9))
>
> Needless to say, this is more complicated (but strangely far easier to
> understand for me). Another solution is to _define_ a new function,
> recursive,
> inside the outer one which thus keeps a simple interface (no 'i' or
> 'sub_res').
> This is very often needed in recursion in functional programming, because
> we
> need to pass partial data anyway (state of partial execution). In fact,
> your
> case and mine are not typical.
>
> Thus, if we must define a new function anyway, we have the choice of
> running
> upwards or downwards; downwards is the normal case for a weird reason,
> namely
> that the typical data structure there is a linked list, to which one can
> only
> (efficiently) put new items at start; thus, if we run forwards, the
> results are
> backwards.
>
> Advice: use recursion whenever it corresponds to the application; meaning
> the
> idea you are expressing itself is recursive. This is the case in problems
> which
> decompose into sub-problems *of the same form*. Otherwise, avoid forcing
> recursion whenever it drives to a distortion of ideas.
>
> There are situations where, like in our 2 cases, a simple conception is a
> linear
> recursion (recursion just mean repetition, literally re-running), while an
> alternative view is of self-similar recursion, like fractals (what we call
> recursion in programming, see self-similarity in wikimedia). I used to
> program
> both views to get used to recursive thinking.
>
> Denis
>
>
> ------------------------------
>
> Message: 3
> Date: Wed, 11 Dec 2013 13:12:18 +0000
> From: Jignesh Sutar <jsutar at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
> Message-ID:
> <
> CACvW2fxL4-MS8mc-JkCSpq3Zn+h0NtY73wCjj_8NucoecVhaeQ at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> >
> > print str(exe_time).split('.')[0]
>
>
> Sorry, I guess my question was why I can't use something similar to below
> on exe_time (of type datetime.timedelta)? Rather than doing string
> manipulation on decimals or colons to extract the same.
>
> now = datetime.now()
> print now.hour
> print now.minute
> print now.year
>
>
> On 11 December 2013 12:43, David Robinow <drobinow at gmail.com> wrote:
>
> > On Wed, Dec 11, 2013 at 5:55 AM, Jignesh Sutar <jsutar at gmail.com> wrote:
> > > Hi,
> > >
> > > I've googled around extensively to try figure this out assuming it
> > should be
> > > straight forward (and it probably is) but I'm clearly missing
> something.
> > >
> > > I'm trying to get the total run time of the program but have the final
> > time
> > > being displayed in a particular format. I.e. without the seconds in
> > > milliseconds decimal points and just to customize it a bit more.
> > >
> > > import time
> > > from datetime import datetime
> > > startTime = datetime.now()
> > > time.sleep(5.1564651443644)
> > > endTime = datetime.now()
> > > exe_time = endTime-startTime
> > >
> > > print type(startTime)
> > > print type(endTime)
> > > print type(exe_time)
> > >
> > > print "startTime: ", startTime
> > > print "endTime:", endTime
> > > print "exe_time: ", exe_time #how to format this to "D Days, HH: MM:
> SS"
> > ?
> > > #exe_time: 0:00:05.156000
> > > #desired 0 Days, 0h: 00:m: 05s
> > >
> > print str(exe_time).split('.')[0]
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20131211/143a5ed5/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 4
> Date: Wed, 11 Dec 2013 14:12:52 +0100
> From: spir <denis.spir at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] recursive function example
> Message-ID: <52A864D4.9060404 at gmail.com>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> On 12/11/2013 09:50 AM, Alan Gauld wrote:
> > Remember that each time mult() is called it creates
> > its own mini-world of variables independent of the
> > previous calls.
>
> That, is a key point.
>
> Denis
>
>
> ------------------------------
>
> Message: 5
> Date: Wed, 11 Dec 2013 13:37:11 +0000
> From: Mark Lawrence <breamoreboy at yahoo.co.uk>
> To: tutor at python.org
> Subject: Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
> Message-ID: <l89ppq$obf$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 11/12/2013 13:12, Jignesh Sutar wrote:
> > print str(exe_time).split('.')[0]
> > Sorry, I guess my question was why I can't use something similar to
> > below on exe_time (of type datetime.timedelta)? Rather than doing string
> > manipulation on decimals or colons to extract the same.
> >
> > now = datetime.now()
> > print now.hour
> > print now.minute
> > print now.year
> >
>
> Old style
>
> print('%02d:%02d:%04d' % (now.hour, now.minute, now.year))
>
> New style
>
> print('{}:{}:{}'.format(now.hour, now.minute, now.year))
>
> Sorry I can never remember the formatting types to go between {} so look
> for them around here
> http://docs.python.org/3/library/string.html#formatstrings
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 118, Issue 53
> **************************************
>
--
Keith Winston
Director, Earth Sun Energy Systems
301-980-6325
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131212/9cf54477/attachment-0001.html>
More information about the Tutor
mailing list