I need algorithm for my task
Denis McMahon
denismfmcmahon at gmail.com
Wed Nov 5 23:00:38 EST 2014
On Thu, 06 Nov 2014 03:36:40 +0000, Denis McMahon wrote:
> On Wed, 05 Nov 2014 18:49:01 +0000, MRAB wrote:
>
>> It doesn't work for the final example or "barbaz".
>
> Oh, and we really need a private "python homework answers" list where we
> can discuss the most pythonic solution we can think of for all these
> homework / coursework questions without showing the solutions to the
> students.
> I'm quite proud of a 2 line solution to the third algorithm that I
> implemented, although it might be considered as somewhat obfuscated code
> by some. ;)
Even prouder of the 1 liner!
In fact I'm going to post it, because I think any student who can walk
his instructor through how my one-liner works deserves the marks. Of
course, any student who presents this as a solution and is unable to walk
their instructor through it should be failed for stupidity and plagiarism!
t = ["pythonpythonpyth", "DOLORIUMD", "HELLOLHELLO", "thewordword",
"barbaz", "dibdibdobdibdibdob"]
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]
for w in t:
print(baseword(w))
p.s. I really want to be in the room when the instructor asks this
student to come up in front of the class and explain how his solution
works!
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list