I need algorithm for my task

Denis McMahon denismfmcmahon at gmail.com
Wed Nov 5 23:39:38 EST 2014


On Thu, 06 Nov 2014 15:14:05 +1100, Chris Angelico wrote:

> 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.

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.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list