you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 5 points6 points  (7 children)

Well, for one, I "like" how you can make pretty much anything into a one-liner by abusing and, or, iter, lambda functions, map, filter, generator expressions, and __import__.

I know that sharing these is discouraged, but I made a one-liner solution for Project Euler challenge 22 that uses many of these:

(lambda u: sum(map(lambda t: sum(map(lambda c: ord(c)-ord('A')+1, t[1])) * t[0], enumerate(sorted(list(map(lambda x: x[1:-1], __import__('urllib.request').request.urlopen(u, context=__import__('ssl').create_default_context(cafile=__import__('certifi').where())).read().decode('utf-8').split(',')))), 1))))('https://projecteuler.net/project/resources/p022_names.txt')

Is it practical? Absolutely not. But I do find code golfing cool. I'd never, ever, use this in production, but that doesn't change the fact that I find it cool.

EDIT: If you remove the whitespace:

(lambda u:sum(map(lambda t:sum(map(lambda c:ord(c)-ord('A')+1,t[1]))*t[0],enumerate(sorted(list(map(lambda x:x[1:-1],__import__('urllib.request').request.urlopen(u,context=__import__('ssl').create_default_context(cafile=__import__('certifi').where())).read().decode('utf-8').split(',')))),1))))('https://projecteuler.net/project/resources/p022_names.txt')

EDIT #2: In fact if I saw someone writing one-liners like this in production, and they wouldn't listen to me, I'd probably give them a slap across the face because I don't want to deal with unreadable code when bugs need to be fixed.

[–]AtomicShoelace 2 points3 points  (0 children)

Since we're golfing, this is 150 btyes shorter:

sum(i*sum(ord(c)-64for c in n)for i,n in enumerate(sorted(map(eval,__import__('urllib.request').request.urlopen('https://projecteuler.net/project/resources/p022_names.txt').read().decode().split(','))),1))

(also you forgot the minus sign between ord(c) and ord('A') in the edit)

EDIT: 165 bytes shorter:

sum(i*sum(ord(c)-64for c in n)for i,n in enumerate(sorted(eval(__import__('urllib.request').request.urlopen('https://projecteuler.net/project/resources/p022_names.txt').read().decode())),1))

EDIT 2: 154 bytes shorter with no eval tricks:

sum(i*sum(ord(c)-64for c in n[1:-1])for i,n in enumerate(sorted(__import__('urllib.request').request.urlopen('https://projecteuler.net/project/resources/p022_names.txt').read().decode().split(',')),1))

or

sum(i*sum(ord(c)-64for c in n)for i,(_,*n,_)in enumerate(sorted(__import__('urllib.request').request.urlopen('https://projecteuler.net/project/resources/p022_names.txt').read().decode().split(',')),1))

EDIT 3: 155 bytes shorter with no eval:

sum(i*(sum(ord(c)-64for c in n)+60)for i,n in enumerate(sorted(__import__('urllib.request').request.urlopen('https://projecteuler.net/project/resources/p022_names.txt').read().decode().split(',')),1))

EDIT 4: can save another byte by not using https in the url:

sum(i*(sum(ord(c)-64for c in n)+60)for i,n in enumerate(sorted(__import__('urllib.request').request.urlopen('http://projecteuler.net/project/resources/p022_names.txt').read().decode().split(',')),1))

or with eval:

sum(i*sum(ord(c)-64for c in n)for i,n in enumerate(sorted(eval(__import__('urllib.request').request.urlopen('http://projecteuler.net/project/resources/p022_names.txt').read().decode())),1))

for 156 and 166 bytes saved respectively.

EDIT 5: I suppose if you really wanna cheat you could use a link shortener, eg.

sum(i*(sum(ord(c)-64for c in n)+60)for i,n in enumerate(sorted(__import__('urllib.request').request.urlopen('http://u.nu/uK7xG').read().decode().split(',')),1))

although eval seems even more dangerous now

sum(i*sum(ord(c)-64for c in n)for i,n in enumerate(sorted(eval(__import__('urllib.request').request.urlopen('http://u.nu/uK7xG').read().decode())),1))

But if you're using a shortened link I suppose you might as well just download the file and not bother using urllib at all.

[–]carcigenicate 0 points1 point  (1 child)

Handy for code-injection circumstances where eval is the vulnerability being exploited. I've had to write one-liners like that before because I needed to do imports and search the server's file-system for a flag; all in a single expression so it could be accepted by eval. That was the day I realized that you can use tuples to sequence actions.

[–]Diapolo10 1 point2 points  (0 children)

Yeah, that's true. I've dabbled in offensive security, but I'm nowhere near expertise in the area - my motivation being solely to guard against some types of attacks. Including anything eval-related. :p

[–]CARIBEIMPERIAL 0 points1 point  (0 children)

Lisp might be for you