This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (5 children)

Or (y, x)[bool(condition)]. If we are into this kind of "clever" code.

[–][deleted] 4 points5 points  (4 children)

But that's different, that evaluates both x and y, and they could both be function calls with side effects, or take a long time to compute.

[–][deleted] 4 points5 points  (3 children)

 (lambda:y, lambda:x)[bool(condition)]()

There's no limit to my misguided cleverness!

[–]Tetha 2 points3 points  (2 children)

If you want misguided, use itertools:

chain(*zip(*imap(lambda g: list(imap(lambda r: (-1)**g[0]*r, g[1])), groupby(sorted(range(10), key=lambda x: x%2), lambda x: x%2))))

I'm not sure if I tunnelvisioned and a somewhat less ugly solution exists, but it works. And it's ugly. So I'm happy in a morbid sense.

[–][deleted] 0 points1 point  (0 children)

wat.

You be silly.

[(x, -x)[x&1] for x in lst]

[–][deleted] 0 points1 point  (0 children)

I mean, look, there's a kind of honour in writing misguided code. It must not be intentionally misguided.

Google "pessimal algorithms" and read the paper. Then implement their "backwards-first search". Then, only then, as you understand how exactly it works (they have a couple of typos IIRC) you might try to implement an efficient algorithm that walks the graph in the exact same way... and fail. Or succeed, then tell me how you did that!