I need algorithm for my task

wxjmfauth at gmail.com wxjmfauth at gmail.com
Thu Nov 6 09:57:23 CET 2014


Le jeudi 6 novembre 2014 08:59:22 UTC+1, Jussi Piitulainen a écrit :
> 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ö'

Well. Congratulations.



More information about the Python-list mailing list