Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'read.': 0.03; 'algorithm': 0.04; 'that?': 0.05; 'abuse': 0.07; 'subject:code': 0.07; '[0,': 0.09; 'integers': 0.09; 'runtime': 0.09; 'cc:addr:python-list': 0.11; 'random': 0.14; '(),': 0.16; 'bonus,': 0.16; 'effect.': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'skip:[ 50': 0.16; 'sorting': 0.16; 'starmap': 0.16; 'subject:simple': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'unicode.': 0.16; "where's": 0.16; 'elements': 0.16; 'wrote:': 0.18; 'library': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'oriented': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'related': 0.29; 'said,': 0.30; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'that.': 0.31; 'towards': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'lists': 0.32; 'quite': 0.32; '(including': 0.33; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'returning': 0.36; "i'll": 0.36; 'list': 0.37; 'list.': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'needed': 0.38; 'rather': 0.38; 'though,': 0.39; 'expression': 0.60; 'length': 0.61; 'challenge': 0.61; 'back': 0.62; 'more': 0.64; 'minutes': 0.67; 'side': 0.67; '8bit%:31': 0.68; 'special': 0.74; 'can:': 0.84; 'itertools,': 0.84; 'obfuscation,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=zQFD8baNoCDoTRhPJIUQXLKlR4hYGGzPJWrRW2feuxw=; b=FTdj8oOqJmAkMJ7iq3bkttNfAPfiV0ITEpjm4OSp2rqdPiIiY0NNZf8V5yshJ5aZBh PZ7Z/7Is74qahhVd7dTQOLeZnNZe2Gc5q/6Z1j6WoyJjtzy2vawNpkv0XlKzkcOCEEBY 1r53ToRI0LKU09NR3M3eQ/Fb1s74VPy+bkiCQ+Oun989AT8WrL+KTKgZmYV7vlYmxG23 JbmU9YSV9f2WBDuluGKar6QypZUTnmJLgAvSamYyx/BwlivBQqrfmi222EFqrLnxp3id i9uzq+BHBKAKG7reB1srY34mqByWrBkclpeeehoaSCeoI1qlkuqaywGll9xukqk8jOh0 HsDg== X-Received: by 10.152.42.171 with SMTP id p11mr4687376lal.79.1372629177979; Sun, 30 Jun 2013 14:52:57 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <51d06cb6$0$29999$c3e8da3$5496439d@news.astraweb.com> References: <51d06cb6$0$29999$c3e8da3$5496439d@news.astraweb.com> From: Joshua Landau Date: Sun, 30 Jun 2013 22:52:17 +0100 Subject: Re: Stupid ways to spell simple code To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372629187 news.xs4all.nl 16011 [2001:888:2000:d::a6]:36213 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49516 On 30 June 2013 18:36, Steven D'Aprano wrote: > Pfft! Where's the challenge in that? Let's use an O(n!) algorithm for > sorting -- yes, n factorial -- AND abuse a generator expression for its > side effect. As a bonus, we use itertools, and just for the lulz, I > obfuscate as many of the names as I can: > > > from random import shuffle as OOO00O > from itertools import takewhile as OO0O0O, count as O0OO0O > > OO0O00 =3D range(5) > > list(OO0O0O(lambda O: any(O[O0] < O[O0-1] for O0 in > range(1, sum(('O' for O in O), type('O', (), > {'__add__': lambda O0O, OO0: OO0})()).count('O'))), > (OOO00O(OO0O00) or OO0O00 for O in O0OO0O())))[0] I've got to say -- that last line got me for quite a while, especially as the list of lists was returning a list of sorted lists! For obfuscation, though, you want unicode. It's much harder to reason about code that you can't read. from itertools import count as =C3=A6, permutations as =C3=86, starmap as = =C3=B0 [globals().setdefault("%c"%sum(=C3=B8.encode()),=C3=9F)for =C3=B8,=C3=9F in vars(__builtins__).items()if =C3=B8!=3D"vars"] sorted =3D lambda =C4=A7:=C6=BF(=C6=9E for =C4=B1 in =CF=86(=C6=B4(lambda:=C4=A7,=C3=A6),=C5=84(0,=C5=84(=C4=A7)**(len(=C4=A7)*2= -1)*len(=C4=A7)))for =C6=9E in =C3=86(=C4=A7)if =C7=82(()).__eq__((=C5=95(=C3=B0(=C7=82(=C6=BC(=C6=9E).pop()).__rpow__,=CF= =86(=C5=95(=C5=93(=C6=9E,=C6=B4(lambda:0,=C4=A7)),()),1))),=C4=A7),=C4=B1))= [::-1] print(sorted([4, -3, 4, 2, -1])) This only works for integers (including negatives) and some things that seem to do nothing but slow it down are needed to cover special cases. This one takes about 3=C2=BD minutes on my computer -- it's got a runtime efficiency of something along the lines of O( (L!) * ((L)**i) ) for i being related to the elements in the list and L being the length of the list. That said, it's rather OK at sorting [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0= ]. I know this is more oriented towards obfuscation of code than obfuscation of technique, but --as I said-- I'm writing a library for that. I'll be back when it's ready.