Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70461

Re: symple programming task

Date 2014-04-21 08:39 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: symple programming task
References <cc416b4b-e19d-49b9-abc2-86bcff455f9c@googlegroups.com> <7f496a70-6b85-4bc9-bf33-7aeaad813d11@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.9406.1398087564.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-04-21 06:21, Ivan Ivanivich wrote:
> > Find the sum of all the multiples of 3 or 5 below 1000.
> my new version of script:
> 
> total = 0
> div1 = 3
> div2 = 5
> for basis in range(0, 1000):
>         mod = basis % div1
>         if mod == 0:
>                 total = total + basis
>                 continue
>         mod = basis % div2
>         if mod == 0:
>                 total = total + basis
>                 continue
> 
> 
> 
> print("total = ", total)

Now that you have a working solution, I don't mind giving my more
pythonic solution:

  sum(dividend for dividend in range(1000)
    if any(dividend % divisor == 0 for divisor in (3, 5)))

which succinctly states the problem and makes it easy to
add/remove/change the divisors in one place rather than having to
define multiple variables to hold them and "if" statements to
evaluate them.

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-20 11:43 -0700
  Re: symple programming task Chris Angelico <rosuav@gmail.com> - 2014-04-21 05:02 +1000
  Re: symple programming task Peter Otten <__peter__@web.de> - 2014-04-20 21:11 +0200
  Re: symple programming task Joel Goldstick <joel.goldstick@gmail.com> - 2014-04-20 15:15 -0400
  Re: symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-20 12:27 -0700
    Re: symple programming task Joshua Landau <joshua@landau.ws> - 2014-04-21 12:43 +0100
  Re: symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-21 06:21 -0700
    Re: symple programming task Tim Chase <python.list@tim.thechases.com> - 2014-04-21 08:39 -0500
    Re: symple programming task Chris Angelico <rosuav@gmail.com> - 2014-04-21 23:43 +1000

csiph-web