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


Groups > comp.lang.python > #41678

Re: problem with function

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'test,': 0.05; '0),': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'index': 0.13; "'a',": 0.16; "'c',": 0.16; "'d',": 0.16; '2),': 0.16; 'letter:': 0.16; 'loops': 0.16; 'passed.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'copied': 0.17; 'jan': 0.18; 'tests': 0.18; 'code,': 0.18; 'subject:problem': 0.22; 'errors': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'header:X -Complaints-To:1': 0.28; 'assert': 0.29; 'index,': 0.29; 'to:addr :python-list': 0.33; 'code:': 0.33; 'version': 0.34; 'pm,': 0.35; 'received:org': 0.36; 'subject:with': 0.36; 'well.': 0.37; 'subject:: ': 0.38; 'possible.': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'gone': 0.64; 'following.': 0.65; 'news': 0.68; 'soon': 0.70; 'exercise,': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: problem with function
Date Thu, 21 Mar 2013 19:20:49 -0400
References <870147F2-AF34-456A-BDF3-12C0A2F67A69@icloud.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-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4
In-Reply-To <870147F2-AF34-456A-BDF3-12C0A2F67A69@icloud.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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3607.1363908076.2939.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363908076 news.xs4all.nl 6959 [2001:888:2000:d::a6]:60999
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41678

Show key headers only | View raw


On 3/21/2013 2:31 PM, leonardo selmi wrote:

> i wrote the following code:
>
> def find(word, letter):
>      index = 0
>      while index < len(word):
>          if word[index] == letter:
>              return index
>          index = index + 1
>      return -1

Since this is a learning exercise, consider the following.

def find(word, letter):
     for index, let in enumerate(word):
         if let  == letter:
             return index
     return -1

for w, l, n in (('abc', 'a', 0), ('abc', 'c', 2), ('abc', 'd', -1)):
     assert find(w, l)  == n
print("no news is good news")

I copied the code, wrote the test, ran it, and it passed. I then 
re-wrote until syntax errors were gone and the new version passed. For 
loops are specialized, easier-to-write version of while loops that scan 
the items of a collection (iterable). Learn them and use them well. 
Learn to write automated tests as soon as possible.

-- 
Terry Jan Reedy

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


Thread

Re: problem with function Terry Reedy <tjreedy@udel.edu> - 2013-03-21 19:20 -0400

csiph-web