Convert any Python file into a single line of code
21–27 of 27 posts
Re: Convert any Python file into a single line of code
#22> Space: O(n). No code is ever duplicated, so the one-lined code produced is linear in the size of the input code. Seems like a non sequitur?
I'm not sure why you think that. Do you mean that normally, in this context "space" means the memory usage of a program, which isn't particularly related to the size of the program? In which case, yeah, it's a little incongruous. The "space" bit is talking about the textual representation of the program produced, while the "time" bit is talking about the runtime characteristics of the program.
Re: Convert any Python file into a single line of code
#23The one big thing I notice this is doing "wrong", is that it is not linear in recursion depth: Each statement increases the stack depth by 1. The way around this is to instead sequence the commands using arrays, since python guarantees the execution order is left to right. So his example could have been rewritten as:
(lambda s: [s.__setitem__('x', 3), s.__setitem__('y', 4), print(s['x']
(though for this example you could just modify the result of globals())Then you can also do trickery to get for and while loops into generator expressions as opposed to lambdas. The hard part is handling continues and breaks. For instance:
while :
can get compiled into something like list( for _ in itertools.takewhile(lambda _: , itertools.cycle([1])))
(though you'll want to replace list with a function that evaluates the iterator but doesn't take up O(N) space where N is the number of iterations, something like a no-op accumulator)To handle breaks and continues, you'd have to make your be aware that a break/continue was called (a flag on some meta table was my plan), so they can terminate early. You'd also have to change to terminate the loop (on a break) and reset the continue flag.
Re: Convert any Python file into a single line of code
#24> Space: O(n). No code is ever duplicated, so the one-lined code produced is linear in the size of the input code. Seems like a non sequitur?
I'm not sure why you think that. Do you mean that normally, in this context "space" means the memory usage of a program, which isn't particularly related to the size of the program? In which case, yeah, it's a little incongruous. The "space" bit is talking about the textual representation of the program produced, while the "time" bit is talking about the runtime characteristics of the program.
Re: Convert any Python file into a single line of code
#25^^ Legendary
Re: Convert any Python file into a single line of code
#26Second try: Your code could not be one-lined. Open problem: yield
Third try: Your code could not be one-lined. Open problem: raise
Fourth try: Your code could not be one-lined. Not yet implemented: classdef
So, any file without class definitions, try/except/raise/yield (and probably some more). Not my definition of 'any' :)
Re: Convert any Python file into a single line of code
#27Earlier quoted context omitted.
haha, I think the resulting one liners are a little over 80 characters :)
Eh, you can insert newlines wherever you want and it'll still work, plus the "80 char line" rule is at the top of the list of PEP8 rules you can break.