[Tutor] Working with strings

alan.gauld@bt.com alan.gauld@bt.com
Mon, 12 Mar 2001 18:16:34 -0000


------_=_NextPart_001_01C0AB20.921AD8B0
Content-type: text/plain; charset="iso-8859-1"

Most replies have tried to give you a different function, I'll try to 
work with this one...  
 
First I'll indent it they way I think you meant it to be and use a non 
proportional font for clarity:
 
def backword(bruce):
    bruce=raw_input('input a word to reverse  ')
    index=len(bruce)-1
    while index<0:
       letter=bruce[index]
       print letter,
       index = index+1 

 I also meant to say that the algorithm above is broken...
    
    index=len(bruce)-1

find index of last letter

    while index<0:
never do the loop stuff because the index of the last letter is greater than
0
exit the function.
 

The algorithm should I think be:
 
index = len(bruce)-1
while index >= 0:
    print bruce[index]
    index = index -1
 
Thus for 'flowerpot' we get an index of 8
which now enters the while loop
Now we can print bruce[-n](ie 't') 
then decrement index to 7 and print 'o' etc..
till index = 0 which prints 'f'But several of the other 
suggested algorithms are better, I just assume this is 
the one in the book...
 
Alan G

------_=_NextPart_001_01C0AB20.921AD8B0
Content-type: text/html; charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>Most 
  replies have tried to give you a different function, I'll try to 
  </FONT></SPAN></DIV>
  <DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>work 
  with this one...</FONT></SPAN>&nbsp;<FONT size=2><FONT face=Arial><SPAN 
  class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN 
  class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
  color=#0000ff>First I'll indent it they way&nbsp;I think you meant it to be 
  and use a non </FONT></SPAN></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
  color=#0000ff>proportional font for clarity:</FONT></SPAN></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN 
  class=360213516-12032001>&nbsp;</SPAN><BR></FONT><FONT 
  face=Arial></FONT></FONT><FONT face="Courier New" size=2>def 
  backword(bruce):<BR><SPAN class=360213516-12032001><FONT color=#0000ff><FONT 
  color=#000000>&nbsp;&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;bruce=raw_input('input 
  a word to reverse&nbsp; ')<BR><SPAN class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;<FONT 
  color=#000000>&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;index=len(bruce)-1<BR>&nbsp;<SPAN 
  class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;&nbsp;&nbsp;</FONT></SPAN>while 
  index&lt;0:<BR>&nbsp;&nbsp;<SPAN class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;&nbsp;&nbsp; 
  &nbsp;</FONT></SPAN>letter=bruce[index]<BR>&nbsp;&nbsp;<SPAN 
  class=360213516-12032001><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; 
  &nbsp;</FONT></SPAN>print letter,<BR>&nbsp;&nbsp;<SPAN 
  class=360213516-12032001><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; 
  &nbsp;</FONT></SPAN>index = index+1</FONT><FONT size=2><FONT face=Arial><SPAN 
  class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV></BLOCKQUOTE>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><SPAN 
class=780560318-12032001><FONT color=#0000ff>&nbsp;I also meant to say that the 
algorithm above is broken...</FONT></SPAN></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><SPAN 
class=780560318-12032001>&nbsp;</SPAN>&nbsp;</SPAN>&nbsp;&nbsp;<BR></FONT></FONT><FONT 
face="Courier New"><FONT size=2><SPAN class=360213516-12032001><FONT 
color=#0000ff><SPAN class=360213516-12032001><FONT color=#0000ff>&nbsp;<FONT 
color=#000000>&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;index=len(bruce)-1</FONT></SPAN></FONT></FONT></DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT size=2><SPAN class=360213516-12032001><FONT color=#0000ff 
  face=Arial>find index of last letter</FONT></SPAN></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
  color=#0000ff><BR><FONT face="Courier New">&nbsp;<SPAN 
  class=360213516-12032001><FONT 
  color=#0000ff>&nbsp;&nbsp;&nbsp;</FONT></SPAN>while 
  index&lt;0:<BR></FONT><FONT face=Arial>never do the loop stuff because the 
  index of the last letter is greater than 
  0</FONT></FONT></SPAN></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
  class=360213516-12032001>exit the function.</SPAN></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><SPAN 
  class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><SPAN class=780560318-12032001><FONT color=#0000ff face=Arial size=2>The 
algorithm should I think be:</FONT></SPAN></DIV>
<DIV><SPAN class=780560318-12032001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=780560318-12032001></SPAN><FONT color=#0000ff 
face="Courier New" size=2><SPAN class=780560318-12032001>index = 
len(bruce)-1</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=780560318-12032001>while index &gt;= 0:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=780560318-12032001>&nbsp;&nbsp;&nbsp; print 
bruce[index]</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=780560318-12032001>&nbsp;&nbsp;&nbsp; index = index -1</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff size=2><SPAN 
class=780560318-12032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>Thus 
for 'flowerpot' we get an index of 8</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>which 
now enters the while loop</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>Now we 
can print bruce[-n](ie 't') </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>then 
decrement index to&nbsp;7 and print 'o' etc..</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>till 
index = 0 which prints 'f'But several of the other </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=780560318-12032001>suggested algorithms are better, I just assume this is 
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=780560318-12032001>the 
one in the book...</SPAN></FONT></DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=780560318-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=780560318-12032001>Alan G</SPAN></FONT></FONT></DIV></BODY></HTML>

------_=_NextPart_001_01C0AB20.921AD8B0--