[Solved] write python code in single line


Since pythons list comprehensions are turing complete and require no line breaks, any program can be written as a python oneliner.

If you enforce arbitrary restrictions (like “order of the statements” – what does that even mean? Execution order? First apperarance in sourcecode?), then the answer is: you can eliminate some linebreaks, but not all.

instead of

if x:
   do_stuff()

you can do:

if x: do_stuff()

instead of

x = 23
y = 42

you can do:

x,y = 23, 42

and instead of

 do_stuff()
 do_more_stuff()

you can do

do_stuff; do_more_stuff()

And if you really, really have to, you can exec a multi-line python program in one line, so your program becomes something like:

exec('''t=int(input())\nwhile t:\n t-=1;n=int(input());a=i=0\n while not(n&1<<i):i+=1\n while n&1<<i:n^=1<<i;a=a*2+1;i+=1\n print(n^1<<i)+a/2\n''')

But if you do this in “real” code, e.g. not just for fun, kittens die.

1

solved write python code in single line