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


Groups > comp.lang.python > #57657

Re: Check if this basic Python script is coded right

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'correct.': 0.07; 'tests.': 0.07; "'return": 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:script': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '3):': 0.16; 'assignment.': 0.16; 'multiples': 0.16; 'n+1,': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'reusable': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'putting': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'assert': 0.31; 'prints': 0.31; 'cases': 0.33; 'basic': 0.35; 'test': 0.35; 'add': 0.35; 'doing': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'university': 0.39; 'received:org': 0.40; 'numbers': 0.61; 'received:173': 0.61; 'first': 0.61; 'sum': 0.64; 'skip:+ 10': 0.65; 'between': 0.67; 'subject:this': 0.83; 'cubes': 0.84; 'received:fios.verizon.net': 0.84; 'subject:Check': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Check if this basic Python script is coded right
Date Sat, 26 Oct 2013 16:16:22 -0400
References <9716695c-31e3-40a0-b2a4-73b967494107@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-59-117-133.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0.1
In-Reply-To <9716695c-31e3-40a0-b2a4-73b967494107@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1605.1382818600.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1382818600 news.xs4all.nl 15884 [2001:888:2000:d::a6]:60839
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57657

Show key headers only | View raw


On 10/26/2013 1:36 PM, HC wrote:
> I'm doing my first year in university and I need help with this basic assignment.
>
> Assignment: Write Python script that prints sum of cubes of numbers between 0-200 that are multiples of 3. 3^3+6^3+9^3+12^3....+198^3=?
>
> My script:
> count = 0
> answer = 0
>
> while count<200:
>      if count%3==0:
>          answer = answer + count**3
>      count = count + 1
>
> print ("Result is: " +str(answer))
>
> Is it all okay?

Generalize the problem, put the code in a reusable testable function, 
and start with test cases.
--
def sum33(n):
   "Return sum of cubes of multiples of 3 <= n."

for n, sm in ( (0,0), (2,0), (3,27), (4,27), (6,243), ):
   assert sum33(n) == sm

print(sum33(200))
--
This will fail because None != 0. Now add 'return 0' and the first two 
cases pass and then it fails with None != 27. Now change 'return 0' to

   answer = 0
   for i in range(3, n+1, 3):
     answer += i*i*i
   return answer

and it will print 131990067, which I presume is correct. Putting your 
code in the body instead, indented, with '200' replaced with 'n+1', and 
with 'return answer' added, gives the same result after passing the same 
tests.

-- 
Terry Jan Reedy

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


Thread

Check if this basic Python script is coded right HC <hikmat@jafarli.net> - 2013-10-26 10:36 -0700
  Re: Check if this basic Python script is coded right Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-26 13:53 -0400
  Re: Check if this basic Python script is coded right Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 18:55 +0100
  Re: Check if this basic Python script is coded right John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-26 10:58 -0700
  Re: Check if this basic Python script is coded right MRAB <python@mrabarnett.plus.com> - 2013-10-26 19:20 +0100
    Re: Check if this basic Python script is coded right rusi <rustompmody@gmail.com> - 2013-10-27 05:20 -0700
      Re: Check if this basic Python script is coded right Tim Delaney <timothy.c.delaney@gmail.com> - 2013-10-28 08:30 +1100
  Re: Check if this basic Python script is coded right Terry Reedy <tjreedy@udel.edu> - 2013-10-26 15:46 -0400
  Re: Check if this basic Python script is coded right Terry Reedy <tjreedy@udel.edu> - 2013-10-26 16:16 -0400

csiph-web