Well, first of all to kick us off, here it is.
(lambda: (lambda p: [print("\033c", end=""), [None, lambda: (lambda nums: print("Numbers:", nums) or print((acc := 1, [acc := acc * x for x in nums], acc)[-1]))([int(i) for i in input("Enter integers separated by a space: ").split()]), lambda: (lambda nums: print("Numbers:", nums) or print([v for v in nums if v != int(input("What should be removed: "))]))([int(i) for i in input("Enter integers separated by a space: ").split()]), lambda: (lambda s: print("New list:", s.replace(input("Enter a value to remove: "), "")))(input("Enter a string: ")), lambda: (lambda s: print("Is", s, "a palindrome?", s == s[::-1]))(input("Enter a string: ")), lambda: print("Number of words:", len(input("Enter a sentence: ").split())), lambda: (lambda n: print("Factors of", str(n) + ":", sorted({f for i in range(1, int(n**0.5) + 1) if n % i == 0 for f in (i, n // i)})))(int(input("Enter a number: "))), lambda: (lambda nums: print("Numbers:", nums) or (lambda s: s.remove(max(s)) or print("Second largest:", max(s)))(set(nums)))([int(i) for i in input("Enter integers separated by a space: ").split()])][p]()])(int((ask := lambda: p if (p := input("Enter 1, 2, 3, 4, 5, 6, or 7: ")) in [str(i) for i in range(1, 8)] else (print("\033cThat's not a valid input!") or ask()))
Is that too hard to read? Okay, we can break it down (or more accurately, first let it wrap lines
(lambda: (lambda p: [print("\033c", end=""), [None, lambda: (lambda nums: print("Numbers:", nums) or print((acc := 1, [acc := acc * x for x in nums], acc)[-1]))([int(i) for i in input("Enter integers separated by a space: ").split()]), lambda: (lambda nums: print("Numbers:", nums) or print([v for v in nums if v != int(input("What should be removed: "))]))([int(i) for i in input("Enter integers separated by a space: ").split()]), lambda: (lambda s: print("New list:", s.replace(input("Enter a value to remove: "), "")))(input("Enter a string: ")), lambda: (lambda s: print("Is", s, "a palindrome?", s == s[::-1]))(input("Enter a string: ")), lambda: print("Number of words:", len(input("Enter a sentence: ").split())), lambda: (lambda n: print("Factors of", str(n) + ":", sorted({f for i in range(1, int(n**0.5) + 1) if n % i == 0 for f in (i, n // i)})))(int(input("Enter a number: "))), lambda: (lambda nums: print("Numbers:", nums) or (lambda s: s.remove(max(s)) or print("Second largest:", max(s)))(set(nums)))([int(i) for i in input("Enter integers separated by a space: ").split()])][p]()])(int((ask := lambda: p if (p := input("Enter 1, 2, 3, 4, 5, 6, or 7: ")) in [str(i) for i in range(1, 8)] else (print("\033cThat's not a valid input!") or ask()))
That should help you see its complete and utter demonic beauty. If you're wondering what that code does, it's part of a class I had to do at school where we learn basic python.
If you've actually... read my site, you know that I'm somewhat overqualified for it, so in order to challenge me, the intern, a senior, who is also USACO Gold challenged me: Code golf it (kind of). Can I do the whole thing, which is meant to be split across files and done with overcomplicated syntax like os.system("clear") and lots of if statements, can I do it in:
- 1 line of code
- 0 import statements
- 0 semicolons
- 100% of the greatest/worst python you may have ever seen?
The answer is yes. Of course yes.
Here's the autopsy of how I killed readability.
Crime #1: The Infinite Recursion Loop
Normal people use while True to create a menu loop, but as I think we've established by now, I'm not normal. Also, while is a statement, not an expression, and lambdas can only contain expressions.
So I created a Self-Defining Recursive Lambda (hereon referred to as an SDRL) inside an assignment expression.
(ask := lambda: p if (p := input("Enter 1-7: ")) in valid_list
else (print("Invalid!") or ask()))
- The Walrus (:=): I define ask inside the line that calls it.
- The
orHack:print()returns None, which is Falsy. Soprint(...) or ask()prints the error and then forces Python to evaluate the right side—calling the function again. Infinite loop achieved.
Pretty neat, huh?
Crime #2: The Dispatch Table
if and else statements are also forbidden inside lambas. Go figure, huh. How do we switch behind the 7 (7? Look, I wrote this code weeks ago and didn't get around to writing this post until now, cut me some slack here)
We use a list as a lookup table (versus dictionaries, python's actual lookup table datatype)
[None, task1, task2, ...][p]()
If the user types "$1$", I index the list at $1$, grab the lambda stored there, and immediately invoke it with (). It’s essentially a jump table, but uglier.
Not-Quite-Crime #3: The "0 Imports" Screen Clear
The assignment required clearing the terminal (implemented by the teacher, but I was re-implementing everything anyways)
Usually, you’d do import os; os.system('clear'). But imports are statements (banned).
So I just printed the raw ANSI escape code for "Reset Terminal":
print("\033c", end="")
It’s faster, cleaner, and strictly follows the "no imports" rule while being arguably more confusing to read.
I put this as a Not-Quite-Crime because it's actually better practice because it is terminal-dependent instead of system-dependent and doesn't call shell commands, which trust me, as a CTF player, I have absolutely path injected clear before, and say this was meant to be a "safe" program that ran with the setuid bit?
Good thing I made it not call a shell command :-)
Crime #4/3: The Side-Effect Accumulator
Is it three? Does Not-Quite-Crime #3 count? Whatever, I'll say this is $4$ anyways. Task #1 was to calculate the product of a list of numbers. Usually, you'd loop. I did this:
(acc := 1, [acc := acc * x for x in nums], acc)[-1]
This is a tuple containing three things:
1. Initialize acc to 1.
1. Run a list comprehension that updates acc for every number (ignoring the resulting list).
1. Return acc``
Then I grab the last item[-1]` to get the result. It turns an imperative loop into a single expression.
By the way, just because I finally added markdown support to the blog, here's the math syntax for that:
$$ \prod_{x \in \text{nums}} x $$
Look at that rendering. Crisp. Now, back to the carnage.
Crime #5: The Optimization Disguised as Garbage
Task #6 was to find the factors of a number. Now, a naive programmer loops from $1$ to $n$. A decent programmer loops to $\frac{n}{2}$.
I, however, decided to actually write an efficient $O(\sqrt{n})$ algorithm, but I hid it inside a set comprehension so it looks like keyboard mash. (Sorry, competitive programmer brain couldn't help it!!! It's not my fault!!)
{f for i in range(1, int(n**0.5) + 1) if n % i == 0 for f in (i, n // i)}
Here's the logic:
1. Loop i from $1$ to $\sqrt{n}$.
1. If i divides n, we add two numbers to the set: i and its pair n//i
1. Why a set and not a list? Because if $n$ is a perfect square (like 36), this logic adds $6$ twice. The set swallows the duplicate automatically (minimalist) even if (at least it would be true in C++), its slightly slower (though python arrays are notoriously slow anyways, I would believe sets are the same speed in python)
It is arguably the only "good" code in this entire script, and I ruined it by removing all the whitespace.
Crime #6: The Destructive Lambda
Task #7 asked for the second largest number in a list, a task that has never ever been asked for before of competitive programmers (slightly different problem, but you get the point... I didn't look that hard, any problem like this would obviously be some sort of set problem normally [update/query] or maybe HLD? idk, its SUCH** a simple concept there aren't really any problems about it) (https://codeforces.com/problemset/problem/1491/A) The normal way: Sort the list, take the second to last index The code golf way: Delete the biggest, find the biggest again
(lambda s: s.remove(max(s)) or print("Second largest:", max(s)))(set(nums))
set(nums): First, I cast to a set to remove duplicates (because if the input is[5, 5, 4], the second largest is 4, not 5).s.remove(max(s)): I literally delete the maximum value from the data structure.or: Again,removereturnsNone, so theorallows us to chain execution to the print statement.max(s): Since the old max is gone, the new max is the second largest.
Is it efficient? No. Does it destroy the data you passed into it? Yes. Does it work? Also yes.
Conclusion
This code handles errors, clears the screen (safely!), and performs 7 different mathematical operations. It also violates every single principle of the Zen of Python.
- Beautiful is better than ugly: Failed. Ugly is king.
- Simple is better than complex: Failed. I don't think a single thing here is simpler rather than complex
- Readability counts: I think I killed Tim Peters with this.
But hey, it runs. And I didn't use a single semicolon.