Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Aghajari/One-Line-Art
One-Line Art with Python =)
https://github.com/Aghajari/One-Line-Art
Last synced: 3 months ago
JSON representation
One-Line Art with Python =)
- Host: GitHub
- URL: https://github.com/Aghajari/One-Line-Art
- Owner: Aghajari
- Created: 2021-11-29T23:48:04.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-01T11:30:56.000Z (almost 3 years ago)
- Last Synced: 2024-06-28T13:32:00.376Z (5 months ago)
- Language: Python
- Size: 62.5 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# One Line Art
**Hello There 👋**
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](https://medium.com/@michellegemmeke/the-art-of-one-line-drawings-8cd8fd5a5af7) :
> 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!**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 😃## [Discussions](https://github.com/Aghajari/One-Line-Art/discussions)
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)
```html
Spoil```py
YOUR CODE HERE
`` `
```
## Table of Contents
- [Lambda Functions](#lambda-functions)
- [Input](#input)
- [Calling Functions](#calling-functions)
- [List Comprehension](#list-comprehension)
- For Loops
- While Loops
- [Recursive Functions](#recursive-functions)
- [Factorial](#factorial)## Lambda Functions
First of all, You should learn about [lambda functions](https://www.w3schools.com/python/python_lambda.asp). 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...](https://realpython.com/python-lambda/#lambda-calculus)```py
>>> (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 :```py
n = int(input())
print(n ** n)
```
But now you have to use lambda functions, see :
```py
print((lambda n: n ** n)(int(input())))
```OR by using [`map()`](https://www.w3schools.com/python/ref_func_map.asp) :
```py
print(*map(lambda n: n ** n, [int(input())]))
```## Calling Functions
You can use lists to call several different functionsLook at this simple code :
```py
a = int(input()) ** 2
a += int(input())
print(a)
```Now Let's do it with a list :
```py
[a := int(input()) ** 2, a := a + int(input()), print(a)]
```## List Comprehension
You can use list comprehension to create a for loop or whileA simple for loop :
```py
for i in range(1, 5):
print(i * i)
```
List Comprehension :
```py
[print(i * i) for i in range(1, 5)]
```A simple while loop :
``` py
a = 0
while a < 10:
a += 1
print(a)
```
List Comprehension :
```py
[(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()`](https://www.w3schools.com/python/ref_func_iter.asp) and [`globals()`](https://www.w3schools.com/python/ref_func_globals.asp)## Recursive Functions
This is a factorial recursive function :
```py
def factorial(x):
return x if x <= 1 else x * factorial(x - 1)>>> factorial(5)
120
```Now Let's do this by lambda functions:
```py
>>> (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
```py
def factorial(x):
return x if x <= 1 else x * factorial(x - 1)print(factorial(int(input())))
```
```py
print((lambda f: lambda x: f(f, x))(lambda f, x: x if x <= 1 else x * f(f, x - 1))(int(input())))
```
2. Using For loop
```py
a = 1
for i in range(1, int(input()) + 1):
a *= iprint(a)
```
```py
print([a := i * globals().get('a', 1) for i in range(1, int(input()) + 1)][-1])
```
3. Using [`functools.reduce()`](https://docs.python.org/3/library/functools.html#functools.reduce)
```py
print(__import__('functools').reduce(lambda x, y: x * y, range(1, int(input()) + 1)))
```
You may want to read more about [`__import__()`](https://docs.python.org/3/library/functions.html#import__)