I need algorithm for my task

Chris Angelico rosuav at gmail.com
Thu Nov 6 05:14:05 CET 2014


On Thu, Nov 6, 2014 at 3:00 PM, Denis McMahon <denismfmcmahon at gmail.com> 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.

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.

ChrisA



More information about the Python-list mailing list