/One-Line-Art

One-Line Art with Python =)

Primary LanguagePython

One Line Art

Hello There 👋

OneLineArt

Have you heard anything about The Art of One Line Drawing Or Single Line Drawing Or maybe Continuous Line Drawing?

From The art of one line drawings :

A one line drawing, also known as a single line drawing, is a drawing made with just one single, unbroken line. The most famous example even dates back to the early 20th century, the one line drawings made by Pablo Picasso. He took a complex, realistic example and simplified it into one single unbroken line. These drawings can look relatively simple to make, but capturing the true essence of the shape in just one line can be quite challenging.

Ain't It Fantastic? 😍
Just as we can draw any simple and complex drawing with one line, we can also implement different algorithms with just one line of code. Obviously, this is not a coding style, but it is clear that creating an algorithm in one line can be very challenging. Therefore, I'm saying that this is literally an art and not everyone can do it!

HelloWorld

Python is one of the best choices to do this. You will soon understand why :)
I will give you some basic ideas on how to do this, then we will share different questions with each other and try to solve it with a single line of code.
It's gonna be very fun and challenging, believe me 😃

I will add various questions over time in the discussion section. Check and send your answer.

  • If you like this topic, you can also help and add new questions to this section.
  • If you want to send an answer, please send your codes in the following format so that the answer to the question is not spoiled for others!
Format (Click to expand)

<details><summary><b>Spoil</b></summary><p>

```py
YOUR CODE HERE
`` `
</p></details>

Table of Contents

Lambda Functions

First of all, You should learn about lambda functions. Lambda functions play an important role in what we want to do

Python lambdas are little, anonymous functions, subject to a more restrictive but more concise syntax than regular Python functions. Read more...

>>> (lambda x, y: x * y)(2, 3)
6
>>> (lambda *args: sum(args))(*range(10, 20))
145

Input

The first challenge you may face is getting input from the user and using it multiple times. Suppose we want to get a number from the user and bring it to the power of the same number. Normally we can do this as follows :

n = int(input())
print(n ** n)

‍‍‍ But now you have to use lambda functions, see :

print((lambda n: n ** n)(int(input())))

OR by using map() :

print(*map(lambda n: n ** n, [int(input())]))

Calling Functions

You can use lists to call several different functions

Look at this simple code :

a = int(input()) ** 2
a += int(input())
print(a)

Now Let's do it with a list :

[a := int(input()) ** 2, a := a + int(input()), print(a)]

List Comprehension

You can use list comprehension to create a for loop or while

A simple for loop :

for i in range(1, 5):
  print(i * i)

List Comprehension :

[print(i * i) for i in range(1, 5)]

A simple while loop :

a = 0
while a < 10:
  a += 1
  print(a)

List Comprehension :

[(a := globals().get('a', 0) + 1, print(a)) for i in iter(lambda: globals().get('a', 0) < 10, False)]

You may want to read more about iter() and globals()

Recursive Functions

This is a factorial recursive function :

def factorial(x):
  return x if x <= 1 else x * factorial(x - 1)

>>> factorial(5)
120

Now Let's do this by lambda functions:

>>> (lambda f: lambda x: f(f, x))(lambda f, x: x if x <= 1 else x * f(f, x - 1))
>>> _(5)
120

Factorial

  1. Using Recursive Functions
def factorial(x):
    return x if x <= 1 else x * factorial(x - 1)

print(factorial(int(input())))
print((lambda f: lambda x: f(f, x))(lambda f, x: x if x <= 1 else x * f(f, x - 1))(int(input())))
  1. Using For loop
a = 1
for i in range(1, int(input()) + 1):
    a *= i

print(a)
print([a := i * globals().get('a', 1) for i in range(1, int(input()) + 1)][-1])
  1. Using functools.reduce()
print(__import__('functools').reduce(lambda x, y: x * y, range(1, int(input()) + 1)))

You may want to read more about __import__()


LCoders | AmirHosseinAghajari
Amir Hossein Aghajari • EmailGitHub