[Tutor] Searching backwards from end of string

Robert Berman bermanrl at cfl.rr.com
Fri Jul 10 22:55:12 CEST 2009



On Fri, 2009-07-10 at 20:55 +0100, Angus Rodgers wrote:
> On Fri, 10 Jul 2009 11:57:21 -0400, Robert Berman
> <bermanrl at cfl.rr.com> wrote:
> 
> >I think you are looking for a complex solution.
> 
> Hardly.
In my opinion your code w was overly complex for what you were
attempting to do.  I would not be so presumptuous to tell you that you
should  change it. I do think you asked for a simple method to solve a
specific problem. That is what I offered; nothing else.
> >How about the following example:
> >
> >
> >In [31]: s1='abcdeefghijkl'  #find last 'e'
> >
> >In [32]: s2=s1[::-1]    #reverses s1
> >
> >In [33]: j=s2.find('e') #finds first 'e' in reversed string
> >
> >In [36]: ind=len(s1)-j-1  #index into s1 where last occurrence of 'e' is
> >
> >In [37]: ind
> >Out[37]: 5
> >
> >In [38]: s1[ind]
> >Out[38]: 'e'
> >
> >In [39]: s1
> >Out[39]: 'abcdeefghijkl' BINGO. Done.
> >
> >Is that not a bit simpler
> 
> I did explain (perhaps at too great a length, or with too many
> irrelevancies):
> 
> >On Fri, 2009-07-10 at 16:24 +0100, Angus Rodgers wrote:
> >> [...]
> >> On the earlier occasion:
> >> [...]
> >> I wrote:
> >> [...]
> >> def rstrip(s):
> >>     return lstrip(s[::-1])[::-1]
> >>     # Note: far from maximally efficient (two unnecessary copies!)
> >> [...]
> >> On the present occasion:
> >> [...]
> >> I thought I had better err in the opposite direction, so I wrote:
> >> [...]
> >> def rfindchr(strng, ch):
> >>     # Surely there's a neater (but still efficient) way?
> >>     n = len(strng)
> >>     for i in range(n - 1, -1, -1):
> >>         if strng[i] == ch:
> >>             return i
> >>     return -1
> 
> I don't even think that's "complicated" (just ugly and clumsy).

I disagree. For the question asked, the solution is complicated. 
A nifty 'notation like "[::-1]"' is an example of something called
slicing which you will find very well explained in 6.1 of CORE PYTHON
PROGRAMMING.  I thought that you had reviewed this since it precedes the
questions in Chapter 6. It is a very handy tool for not only strings,
but lists and dictionaries as well.

> 
> I just wondered if there were some simplifying feature of Python
> that I had either forgotten or not learned about yet.  Python code
> (even mine!) is usually neater than this.
> 
> I know efficiency is not always a major concern (and it certainly
> isn't of any practical importance in a toy example like this),
> but it seems downright profligate to make a reversed copy of a
> string just in order to avoid searching it backwards, and to make
> use of a nifty notation like "[::-1]" - even though my own first 
> instinct is to do exactly that, to save "development time", and
> to make use of a previous solution.

In any case, whether you choose to incorporate my suggestion or not, I
hope you will take some time and experiment with it if only because it's
a fun type of solution.

Robert



More information about the Tutor mailing list