Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11928
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.011 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'backwards': 0.07; 'counting': 0.09; 'none:': 0.09; 'substring': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'slicing': 0.16; 'string:': 0.16; 'subject:stop': 0.16; 'wrote:': 0.16; 'meant': 0.17; "wouldn't": 0.17; 'subject:problem': 0.19; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'though.': 0.23; 'pm,': 0.24; 'starts': 0.24; 'variable': 0.24; 'aug': 0.24; 'code': 0.25; 'string': 0.26; 'loop': 0.28; 'sat,': 0.28; 'message-id:@mail.gmail.com': 0.29; 'calculated': 0.30; 'there': 0.33; 'to:addr:python-list': 0.33; 'right,': 0.34; 'setting': 0.34; 'explicit': 0.34; 'rather': 0.35; 'issue': 0.36; 'example,': 0.37; 'none': 0.37; 'bother': 0.37; 'skip:i 30': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'ways': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'reverse': 0.73; 'subject:value': 0.84; 'ugly,': 0.84; 'original.': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=oLN8hMITf0y3124YOMZVvoDDXnMxfxalyYxPCRqqvaY=; b=i6QyrPQ7WqVt0uxFA2qeGrrl1vqyLK/3zKnweuC7hUnxtGw6Y38mHCGn9geea/OfhV 0CSKKTGJgXwQIleLVwX3EgMqDL/peQ7p9azD2J130feFzWAp0aZDbNU1YmdprqBCa0Jz TnHfAffQWuhNHmA1hKHBNwdcvpo8Pt/CpuYBo= |
| MIME-Version | 1.0 |
| In-Reply-To | <9c3643a8-ba91-4b0b-8144-f4a6260d6cf0@j14g2000prh.googlegroups.com> |
| References | <CAOVPiMgP2kOfr83GK5uOo_-FJOPsrHJW-O_fGgnwkKfBSEQTbw@mail.gmail.com> <mailman.270.1313864985.27778.python-list@python.org> <9c3643a8-ba91-4b0b-8144-f4a6260d6cf0@j14g2000prh.googlegroups.com> |
| Date | Sat, 20 Aug 2011 20:53:53 +0100 |
| Subject | Re: extended slicing and negative stop value problem |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| 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.271.1313870035.27778.python-list@python.org> (permalink) |
| Lines | 30 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1313870035 news.xs4all.nl 23851 [2001:888:2000:d::a6]:35611 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:11928 |
Show key headers only | View raw
On Sat, Aug 20, 2011 at 7:52 PM, Max <maxmoroz@gmail.com> wrote:
> That doesn't work if it's set in a loop or if it's calculated as a
> formula. For example, this very simple code doesn't work because of
> the "-1 problem".
>
Right, which is what I meant by setting it to an explicit None:
if input[starting_pos:ending_pos+1] == input[ending_pos :
starting_pos-1 if starting_pos >= 0 else None : -1]:
You're right that it starts to get ugly, though.
Of course, there are other ways to find the longest palindromic
substring in a string:
# I wouldn't bother counting a one-character "palindrome"
for substr_length in range(len(input),1,-1):
for starting_pos in range(len(input)-substr_length+1):
ending_pos = starting_pos + substr_length - 1
testme = input[starting_pos:ending_pos+1]
if testme == testme[::-1]:
print(testme)
exit(0)
That is, snip out the string and then reverse that snipped piece,
rather than reverse-slicing from the original. This doesn't solve the
issue of slicing backwards with variable halts, though.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: extended slicing and negative stop value problem Chris Angelico <rosuav@gmail.com> - 2011-08-20 19:29 +0100
Re: extended slicing and negative stop value problem Max <maxmoroz@gmail.com> - 2011-08-20 11:52 -0700
Re: extended slicing and negative stop value problem Chris Angelico <rosuav@gmail.com> - 2011-08-20 20:53 +0100
Re: extended slicing and negative stop value problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-21 06:40 +1000
Re: extended slicing and negative stop value problem Max <maxmoroz@gmail.com> - 2011-08-21 10:27 -0700
Re: extended slicing and negative stop value problem Chris Rebert <clp2@rebertia.com> - 2011-08-21 15:16 -0700
csiph-web