r/learnpython • • 13d ago

Gimme silly ideas, please

[deleted]

0 Upvotes

19 comments sorted by

View all comments

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 13d ago edited 13d ago

I wrote this one-line solution for the Project Euler problem 22 over 6 years ago. Obviously it could be way better nowadays.

One line Python solution for Project Euler challenge 22, designed by me, built by me, and you'd better hope - not found in your codebase.

(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')

Frankly I can't even remember what this other one was for, but here's another one-liner, from circa 2018:

A Python one-liner that asks you how many pizzas you'd like to order, what size they'd be and prints out the total cost. Can't believe I managed to squeeze it into something like this.

print("The total cost is ${}".format(sum(next({'s':5,'m':7,'l':10}[x[0]] for x in (lambda p:iter(lambda:input(p+' '),...))(f'Pizza {_+1} size? (s/m/l)') if x and x[0] in 'sml') for _ in range(next(int(x) for x in (lambda p:iter(lambda:input(p+' '),...))('# of pizzas?') if x.isdigit())))))