[Tutor] Need help w/ a for loop

Richard Lovely roadierich at googlemail.com
Thu Oct 23 18:40:53 CEST 2008


Why not throw in itertools.cycle while you're at it? ;-)

pi = sum(4. / (1+x) * itertools.cycle((1,-1)).next() for x in range(0,
4 * n, 2))

I'd also be so tempted just to call the file 'approximate' (read it
with extension...)

Let's also not forget about integer division...

2008/10/23 bob gailer <bgailer at gmail.com>:
> Monte Milanuk wrote:
>
> Hello all,
>
> New guy here, so go easy on me ;)
>
> We save the whips and chains for the more hardened questers. Newcomers get
> the feathers.
>
>
> I'm starting to work my way through Python Programming by Zelle, and have
> hit a bit of a wall on one of the programming exercises in Chapter 3 (#15 if
> anyone has the book handy).
>
> What the question ask is:  Write a program that approimates the value of pi
> by summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11+...
> The program should ask the user for 'n', the number of terms to sum, and
> then output the sum of the first 'n' terms of this series.
>
> Where I am running into problems is how to do the '-' & '+', depending on
> the value of 'n'.  i.e. if 'n' = 3, it's going to be a - & an +, if 'n' =5
> its going to be -, +, -, +, etc.  How to make that work in terms of an
> algorithm is making my head hurt (and its so early in the book yet... ;) )
>
> There are many ways to handle this. Others have given some hints.
>
> The simplest IMHO is to set the range stride to 4 instead of 2 and then use
> x += 4.0/i - 4.0/(i + 2).
>
> You could also use a multiplier (let's call it m) that alternates between 1
> and -1. Roughly:
>
> x = 0
> m = 1
> for in in range...
>   x += 4.0/i*m
>   m =  -m
>
> For more generality and anticipating more complex algorithms:
>
> import operator
> ops = (operator.add, operator.sub)
> x = 0
> m = 0
> for in in range...
>   x = ops[m](x, 4.0/i)
>   m =  1-m
>
> And just for the heck of it you could write 2 for loops, each with a stride
> of 4. The first would just add all the fractions to be added and the second
> would add all the fractions to be subtracted, then combine them. Throwing in
> the sum function and generator expressions:
> pi = sum(4.0/i for i in range(1, n*2, 4)) - sum(4.0/i for i in range(3, n*2,
> 4))
>
> Applying sum and generator expressions to my original solution you get:
> pi = sum(4.0/i - 4.0/(i + 2) for i in range(1, 4*n, 4))
>
> Ah I can go on can't I? A lot more than you asked for!
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> When we take the time to be aware of our feelings and
> needs we have more satisfying interatctions with others.
> Nonviolent Communication provides tools for this awareness.
> As a coach and trainer I can assist you in learning this process.
> What is YOUR biggest relationship challenge?
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


More information about the Tutor mailing list