ONE LINER PYTHON

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
JUST FOR FUN, NOT EVEN A CHALLENGE

I have a very simple Python3 script here
Python:
import sys
def fib(n):
    return n if n <= 1 else fib(n - 1) + fib(n - 2)
print(fib(int(sys.argv[1])))

Before you start telling me this is a very inefficient fibonacci sequence implementation. We are not interested in this. This is just a demonstration of a multi-line Python3 script. While there are some cases where a Python script can be written in one-liner such as
Python:
for i in range(10): print(i)

The challenge here is given any possible simple/complex Python3 script, can it be somehow massaged into yet another valid Python3 script such that it is a valid one liner Python3 source code and can be interpreted by a Python3 interpreter and executed as follows
Bash:
$ python3 oneliner.py

The contents of oneliner.py should be in the form
CCCCCCCCCC....CCCCCCCCC with no newline, no carriage return characters
C here refer to just any other possible characters except "\n" (ASCII 10) or "\r" (ASCII 13)

Below is a sample output when I execute the oneliner.py which will output as per how the fibonacci implementation works
Python:
$ python3 oneliner.py 10
55
 
Last edited:

jgyy1990

Arch-Supremacy Member
Joined
Apr 28, 2012
Messages
12,422
Reaction score
88
I see nothing wrong with writing one liner function regardless it is memory efficient or not. Not sure who will come in and dispute that.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Where are all the new age Python gurus ? Not so sure if we need gurus for this fun question...

:cool:
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
It looks similar to questions from those professors who like to cook their students' brain. Students need to so hard until their brain overheated. :rolleyes:
This to me is not even a challenge, at most fun because seriously there isn't much arcane technique to it. If this can cook a brain; that brain must be very delicious.
I could have applied the same technique to quite a few programming languages out there, but few requires strict indentation like Python.

However there is a messy way to solve it and a comprehensive way to solve it. The comprehensive one actually is the simpler one.

The same technique can be done in Perl too, but it is not exciting to do it for Perl since Perl syntax doesn't normally enforce multilines with indentation.

:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Gosh I keep reading in this forum how Python is like the greatly sought after programming language of the new AI age that we are ushering in, yet there is no contender for this small little Python fun ?

Hint
Code:
696d706f727420737973

I was thinking this is just a small leap.
@peterchan75 not going to take a leap of faith ?
Inviting @Trader11 @project_00 @diminishin @ng min teck @jgyy1990 etc...



:)
 

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Gosh I keep reading in this forum how Python is like the greatly sought after programming language of the new AI age that we are ushering in, yet there is no contender for this small little Python fun ?

Hint
Code:
696d706f727420737973

I was thinking this is just a small leap.
@peterchan75 not going to take a leap of faith ?
Inviting @Trader11 @project_00 @diminishin @ng min teck @jgyy1990 etc...



:)

exec("import sys\ndef fib(n):\n\treturn n if n <= 1 else fib(n - 1) + fib(n - 2)\nprint(fib(int(sys.argv[1])))")
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
Gosh I keep reading in this forum how Python is like the greatly sought after programming language of the new AI age that we are ushering in, yet there is no contender for this small little Python fun ?

Hint
Code:
696d706f727420737973

I was thinking this is just a small leap.
@peterchan75 not going to take a leap of faith ?
Inviting @Trader11 @project_00 @diminishin @ng min teck @jgyy1990 etc...



:)

Nah.. the glory is not worth the leap. :oops:

BTW, my university mate working in NYC investment bank already embarked on a project of ditching perl for python. It looks like the day for perl is numbered.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
exec("import sys\ndef fib(n):\n\treturn n if n <= 1 else fib(n - 1) + fib(n - 2)\nprint(fib(int(sys.argv[1])))")

Escaping work indeed. That is what I call a messy technique.

How about exploring and explaining how you can make this work for any arbitrary python source code?

:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Nah.. the glory is not worth the leap. :oops:

BTW, my university mate working in NYC investment bank already embarked on a project of ditching perl for python. It looks like the day for perl is numbered.

The glory is for you to recognise.

Indeed the days of Python was numbered since 1991, and Perl since 1987

If there is something that Python is good for, it is good for its C/C++ libraries.

I pick Perl over Python any days except when there is a library much better developed in the Python ecosystem.

:)
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Escaping work indeed. That is what I call a messy technique.

How about exploring and explaining how you can make this work for any arbitrary python source code?

:)
Actually I'm confused, the one line condition does it include the imports?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Actually I'm confused, the one line condition does it include the imports?

yes imports are also part of the source code. That is why base64 is out. Your escaping technique works, I have it too as part of my solution, but I find it potentially error prone as I cannot be absolutely sure it will work in all possible rendition of the python source code permutation.

:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
@diminishin

Consider this code
Code:
$ python fib.py 10
55
HELLO    WORLD
    AGAIN
HELL    WORLD
我们!!!!好。

$ cat fib.py
import sys
def fib(n):
    return n if n <= 1 else fib(n - 1) + fib(n - 2)
print(fib(int(sys.argv[1])))
print("HELLO\tWORLD\n\tAGAIN")
print("HELL\tWORLD")
print("\"你们\t大家好。\r我们!!!!")

$ od -c fib.py
0000000    i   m   p   o   r   t       s   y   s  \r  \n   d   e   f
0000020    f   i   b   (   n   )   :  \r  \n  \t   r   e   t   u   r   n
0000040        n       i   f       n       <   =       1       e   l   s
0000060    e       f   i   b   (   n       -       1   )       +       f
0000100    i   b   (   n       -       2   )  \r  \n   p   r   i   n   t
0000120    (   f   i   b   (   i   n   t   (   s   y   s   .   a   r   g
0000140    v   [   1   ]   )   )   )  \r  \n   p   r   i   n   t   (   "
0000160    H   E   L   L   O   \   t   W   O   R   L   D   \   n   \   t
0000200    A   G   A   I   N   "   )  \r  \n   p   r   i   n   t   (   "
0000220    H   E   L   L   \   t   W   O   R   L   D   "   )  \r  \n   p
0000240    r   i   n   t   (   "   \   "  你  **  **  们  **  **   \   t
0000260   大  **  **  家  **  **  好  **  **  。  **  **   \   r  我  **
0000300   **  们  **  **  !  **  **  !  **  **  !  **  **  !  **  **
0000320    "   )  \r  \n
0000324

$ ./generate_python_onliner.sh fib.py # it will generate fib.py.oneliner
$ wc -l fib.py.oneliner # curious why there is no line ???
       0 fib.py.oneliner
$ wc -c fib.py.oneliner $ yet there is 248 characters
     248 fib.py.oneliner

$ python fib.py.oneliner 10
55
HELLO   WORLD
        AGAIN
HELL    WORLD
我们!!!!好。

$ python fib.py.oneliner 10 | less
55
HELLO   WORLD
        AGAIN
HELL    WORLD
"你们   大家好。^M我们!!!!
(END)

You will then see how messy the source code get if you are trying to escape an arbitrary string and stuff into another set of source code inside a string. Newline is not the only thing you need to escape.
I may have anticipated alot, but I may have missed some arcane ones that I didn't anticipate. The cleaner method will handle an arbitrary string just fine.

:)
 
Last edited:

Clarence NMT

Supremacy Member
Joined
Dec 28, 2012
Messages
8,391
Reaction score
2,789
Not sure about asking.

I think above mebtion about 1 liner is bad , if more than 80 or 200 character per line , can't remember. Cos my professor minus 1 mark if overflow certain value, which i find like no point.

I would like see rust for AI. Rust with bean like spring boot and rust for kafka apache. This will change alot finance stuff main language to rust.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Not sure about asking.

I think above mebtion about 1 liner is bad , if more than 80 or 200 character per line , can't remember. Cos my professor minus 1 mark if overflow certain value, which i find like no point.

I would like see rust for AI. Rust with bean like spring boot and rust for kafka apache. This will change alot finance stuff main language to rust.

One liner is not bad if you know when to use it.
The "fun challenge" here is not about appropriateness, it's about knowing how to do it. The key here is transformation, one liner is just a reason. I could have asked for an encrypted source code that and sent over the network if I really wanted to.

Code:
$ openssl aes256 -e -salt -pbkdf2 -in fib.py | base64
U2FsdGVkX1/dAaj3YmU6zQUsR/2jjaqequESYtPPutJ5mo0Hj7shE2n6aY7U3/Thhoh5OgmaV6sRApXdABcm6AClLurWdYGa2ZzmJ53jbXGi3BvMUgT1geZ31gvcuZyVMSFtjQFf9A/9XpNbgpZeJ1lSKno+Zb+SAsLO3QIkzY3g99lAC1GnasCu6udHPCD1xhCT/fWqKQk5piwdeOo34dl9IdSBad+dlm21u4fdyo003QNKbq/rnXO2zGvvL6r8DsMohYeARLypzqiI25KDqKU2xxiIUiKivJobGuKvuDu8sIhaFJq4+ilkWtHO5CFA

The same fib.py but encrypted.

The main thing about AI is not about which programming language to use. No doubt better performance PLs produce faster output. If so, many would swear by C/C++. In fact, why even do that when you could simply employ FPGA or dedicated NPU. Also the kind of domain experts in that area is not about PLs. It's about algorithms, mathematics, and techniques.

:)
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
exec(bytes.fromhex("696d706f7274207379730d0a64656620666962286e293a0d0a2020202072657475726e206e206966206e203c3d203120656c736520666962286e202d203129202b20666962286e202d2032290d0a7072696e742866696228696e74287379732e617267765b315d292929").decode("utf-8"))
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
exec(bytes.fromhex("696d706f7274207379730d0a64656620666962286e293a0d0a2020202072657475726e206e206966206e203c3d203120656c736520666962286e202d203129202b20666962286e202d2032290d0a7072696e742866696228696e74287379732e617267765b315d292929").decode("utf-8"))
That is for you to find a good application.

The significance is does one even knows how to go about solving such kind of problems ? Apparently it seems like something to be taken granted for isn't it ?
Clearly you didn't use this cleaner method as your first try isn't it ?

Here is what I have
Bash:
#!/usr/bin/env bash

## METHOD 1 USING HEX ENCODED STRING. CLEAN AND SIMPLE
SOURCE=$(xxd -p <"$1" | tr -d '\n')
SRCCODE="exec(bytearray.fromhex('$SOURCE').decode())"
printf '%s' "$SRCCODE" >"$1.oneliner"

## METHOD 2 USING ESCAPING. MESSY LOOKING
## UNSAFE SINCE I CANNOT BE SURE THIS METHOD WILL COVER
## ALL POSSIBLE STRING ENCODING THAT PYTHON SOURCE CODE CAN SUPPORT
## THE ONLY ADVANTAGE I CAN THINK OF IS SMALLER SOURCE CODE
## AND SOMEWHAT READABLE SOURCE CODE AND LESS BYTEARRAY.FROMHEX CONVERSION
# SOURCE=$(cat "$1")
# SOURCE=$(gsed -z "s/\\\\/\\\\\\\\/g;s/\n/\\\n/g;s/\r/\\\r/g;s/\"/\\\\\"/g" <<<"$SOURCE")
# SRCCODE="exec(\"$SOURCE\")"
# printf '%s' "$SRCCODE" >"$1.oneliner"

:)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top