[Python] One-liner Stalin sort with 50 characters

Introduction

What is Stalin Sort?

A sort algorithm that realizes the amount of calculation of $ O (n) $ by ** purging (removing) elements that are not in ascending order (descending order)? It seems that it became a hot topic last year, but I didn't meet.

You can write very neatly with Python3.8 or later.

Implementation

stalin_sort = lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]

motion

arr = [1, 2, 1, 1, 4, 3, 9]
print(stalin_sort(arr))
#---> [1, 2, 4, 9]

length

print(len('lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]'))
#---> 50

Summary

It's an implementation that ignores the existence rather than the purge, but it's short and nice

Recommended Posts

[Python] One-liner Stalin sort with 50 characters
Segfault Python with 33 characters
Sort huge files with python
[Python] Sort
Python # sort
Output color characters to pretty with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 15th: Sorting (selection sort)
Create an image with characters in python (Japanese)
Algorithm learned with Python 17th: Sorting (bubble sort)
One-liner that outputs 10000 digits of pi with Python
[Python] How to handle Japanese characters with openCV
Serial communication with Python
Segfault with 0 characters with gcc
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Python beginners challenge Cpaw CTF Q14 with bubble sort
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Bubble sort in Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Python self-made class sort
Posting tweets with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Operate Kinesis with Python