[Tutor] Re:-Recursive Functions

Evripides Loizides loizie@hotmail.com
Sun Jun 1 20:15:03 2003


<html><div style='background-color:'><DIV>
<P># we have the word house we can find the last character like taht:<BR>word = "house"<BR>length = len(word)<BR>lastLetter = word[length - 1]<BR>print lastLetter</P>
<P><BR>&gt;&gt;&gt; e</P>
<P># we have the word "house" we can print all the letters:<BR></P>
<P>word = "house"<BR>index = 0<BR>while index &lt; len(word):<BR>&nbsp;&nbsp; letter = word[index]<BR>&nbsp;&nbsp; print letter,<BR>&nbsp;&nbsp; index = index +1</P>
<P><BR>&gt;&gt;&gt;&gt; h o u s e<BR></P>
<P>i got taht right too but using while loop if u ask me to do it again using recursive function i will not be able.<BR></P></DIV>
<DIV></DIV>&gt;From: Danny Yoo <DYOO@HKN.EECS.BERKELEY.EDU>
<DIV></DIV>&gt;To: Tutor <TUTOR@PYTHON.ORG>
<DIV></DIV>&gt;CC: loizie@hotmail.com 
<DIV></DIV>&gt;Subject: Re: [Tutor] -Recursive Functions (fwd) 
<DIV></DIV>&gt;Date: Sun, 1 Jun 2003 16:17:36 -0700 (PDT) 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; def stringToNum(aString): 
<DIV></DIV>&gt; &gt; (i have to put a condition over here .) 
<DIV></DIV>&gt; &gt; 
<DIV></DIV>&gt; &gt; if myString[n-1].isdigit(): 
<DIV></DIV>&gt; &gt; sum=sum+int(myString[n-1]) 
<DIV></DIV>&gt; &gt; n=n+1 
<DIV></DIV>&gt; &gt; print sum 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Has your instructor given examples of doing recursion across string 
<DIV></DIV>&gt;sequences yet? When we do recursion across a sequence, we often take care 
<DIV></DIV>&gt;of two particular situations: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 1. How do we deal with the "empty" or null sequence? 
<DIV></DIV>&gt; 2. How do we deal with the nonempty sequence? 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;For example, say that we're trying to write a function that can figure out 
<DIV></DIV>&gt;how long a string is. (Pretend that we don't have the len() function 
<DIV></DIV>&gt;handy for the purposes of this exercise). How can we approach a problem 
<DIV></DIV>&gt;like this? 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;One thing to see is that the length of the empty string is zero, so we can 
<DIV></DIV>&gt;code for this: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt;def length(s): 
<DIV></DIV>&gt; "Returns the length of sequence s." 
<DIV></DIV>&gt; if s == "": return 0 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;That was easy. And this handles empty strings perfectly well. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; &gt;&gt;&gt; length("") 
<DIV></DIV>&gt;0 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;The problem, of course, is that it can't handle much else. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; &gt;&gt;&gt; length("hello") 
<DIV></DIV>&gt; &gt;&gt;&gt; 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;So how to we handle strings that aren't empty? Well, we can lop off the 
<DIV></DIV>&gt;first character of any string by doing a slice: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; &gt;&gt;&gt; msg = "Hello" 
<DIV></DIV>&gt; &gt;&gt;&gt; msg[1:] 
<DIV></DIV>&gt;'ello' 
<DIV></DIV>&gt;### 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;How does this string chopping help, though? It helps because the length 
<DIV></DIV>&gt;of the word "Hello" is just one more than the length of the word "ello". 
<DIV></DIV>&gt;More formally: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; length("hello") equals 1 + length("ello") 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Does this get us anywhere? Yes, because we can apply the chopping 
<DIV></DIV>&gt;technique again: the length of "ello" is just 1 + the length of "llo". 
<DIV></DIV>&gt;And the length of "llo" is just 1 + the length of "lo"... 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;So if our length() function can handle strings of length 0, and if we can 
<DIV></DIV>&gt;teach it how to handle nonempty strings, then it should be able to handle 
<DIV></DIV>&gt;all strings. The part that's "recursive" about the length function is 
<DIV></DIV>&gt;that we can write it in terms of a small version of the problem: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; length("some string") = 1 + length("ome string") 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; "big problem" can be reduced to trivial solution, 
<DIV></DIV>&gt; combined with solution to slightly easier 
<DIV></DIV>&gt; problem. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Does this make sense so far? Try writing the length() function and see if 
<DIV></DIV>&gt;it works for you; you'll be better equipped to handle your original 
<DIV></DIV>&gt;problem once you can do length(). 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Good luck to you. 
<DIV></DIV>&gt; 
<DIV></DIV></div><br clear=all><hr>MSN 8 helps <a href="http://g.msn.com/8HMOENUS/2752??PS=">ELIMINATE E-MAIL VIRUSES.</a> Get 2 months FREE*.</html>