The WORST sorting algorithm possible-In just 1 line! (random sort)
h
rafrafraf (1370)

This is my sorting algorithm came up with called random sort - the absolute worst sorting algorithm I could make

the art of python one liners
Python's PEP8 style guide suggests that you should limit all lines to a maximum of 79 characters... 529 isnt too far off

As the name suggests, the algorithm randomly arranges the list until it finally ends up in order

The biggest challenge was doing this all in one line..

  • That's because I had to firstly created a randomly sorted list
  • Then made a check to see if its in order as an extra challenge instead of using the built-in "sorted" function and comparing it
  • But in the end it came together pretty nicely

Roughly how i went about making it inline:

  • the walrus operator (introduced in python 3.8) was a life saver - it allows you to declare variable in if statements (and while loops)
  • lambda
  • inline ifs and fors :)

..AND there is no semi-colons, so this is truly a one liner

I think we all know that python was created to be used illegibly... duhhh

Important notes:

  • if you enter more than 4 keys this program will likely have a recursion error because of how inefficient random sort is
  • enter the values with commas and no brackets[]

Let me know what i should make in 1 line next

You are viewing a single comment. View All
MarcusWeinberger (680)

bruh this is a very cool one-liner damnn even with the walrus operator too