Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jussi Piitulainen Newsgroups: comp.lang.python Subject: Re: I need algorithm for my task Date: 06 Nov 2014 09:59:09 +0200 Organization: A noiseless patient Spider Lines: 36 Sender: jpiitula@ruuvi.it.helsinki.fi Message-ID: References: <1962053862.5573720.1415210715670.JavaMail.zimbra@estudiantes.uci.cu> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="da42b2e96e23bc971f06525f495b78b4"; logging-data="30337"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Y0wh/9DoVznvP86iBxzXBZzxe1fRApSo=" User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Cancel-Lock: sha1:GekI7aC9l/fc9SpB+AnjMk8xs8U= Xref: csiph.com comp.lang.python:80318 Denis McMahon writes: > On Thu, 06 Nov 2014 15:14:05 +1100, Chris Angelico wrote: > > On Thu, Nov 6, 2014 at 3:00 PM, Denis McMahon wrote: > >> def baseword(s): > >> """find shortest sequence which repeats to generate s""" > >> return s[0:["".join([s[0:x]for k in range(int(len(s)/x)+1)])[0:len > >> (s)]for x in range(1,len(s)+1)].index(s)+1] > > > > That's hardly a PEP-8 compliant line, but I can help out a bit. > > > > return s[0:[(s[0:x]*(len(s)//x+1))[0:len(s)]for x in > > range(1,len(s)+1)].index(s)+1] > > > > That's still 83 characters without indentation, but it's close now. > > l = len > r = range > > but technically then it's no longer a one liner. > > > I love the algorithm. Took me a bit of analysis (and inspection of > > partial results) to understand what your code's doing, but it's stupidly > > elegant and elegantly stupid. > > :) > > Well yes, building that list is a stupid way to solve the problem, but I > can't see another way to do it in one line. It's an implementation of my > algorithm 3 (which I think you described) but working from the other end > as it were. 70-character expression, even with the spaces: >>> x = 'tästäkötästäkötästä' >>> x[:min(k for k in range(len(x) + 1) if x == (x[:k] * len(x))[:len(x)])] 'tästäkö'