Perl vs. Python One-Liner
May 20th, 2007A few years ago a friend of mine asked me the following Perl riddle. Unfortunately, in order to solve it you must know Perl. As I like Python much better, I translated the riddle to Python. Attached are both versions.
I admit the Perl version is a bit more cryptic and if you know both Perl and Python you should try to solve the Perl version (but use Python for everything else in life
).
Oh, and try to solve the riddle without running it (run it only as a last resort).

perl -wle 'print "True" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' [number]

python -c "import sys, re; print None == re.match('^1?$|^(11+?)\\1+$','1'*int(sys.argv[1]))" [number]
To alleviate all doubt – [number] denotes a numeric command line argument (e.g. 17).
May 22nd, 2007 at 5:02 pm
(Don’t look before you have solved the above riddle)
What does the next python one-liner function stands for?
lambda n,l=[]:l.append(lambda k,x:(k==1)*[1]or(k%x==0 and[x]+l[0](k/x,x))or l[0](k,x+1))or l[0](n,2)[:-1]
Also, try to write a shorter one-liner with the same functionallity
May 22nd, 2007 at 5:05 pm
actually, a shorter function would be:
lambda n,l=[]:l.append(lambda k,x:(k==x)*[x]or(k%x==0 and[x]+l[0](k/x,x))or l[0](k,x+1))or l[0](n,2)