Interesting/humorous algorithms


ible

professional vim user
Joined
Mar 24, 2014
Messages
2,591
Location
Seattle, WA
everything here is pretty amusing:
http://www.dangermouse.net/esoteric/

one of my favorites: dropsort is a revolutionary O(n) sorting algorithm that iterates through an array, simply dropping elements which are not sorted:
http://www.dangermouse.net/esoteric/dropsort.html

as a way to make it actually do "non-lossy" sorting, you can keep track of dropped items and recursively dropsort them, then merge with the original (undropped) list. here's a python code, for sh*ts and g*ggles.

Code:
def dropsort(A):
    len_A = len(A)
    if len_A == 0:
        return []
    A_sorted = [A[0]]
    A_dropped = []
    for i in range(1, len_A):
        if A[i] >= A_sorted[-1]:
            A_sorted.append(A[i])
        else:
            A_dropped.append(A[i])
    A_dropped = dropsort(A_dropped)
    return merge(A_sorted, A_dropped)

where the merge function is left to the reader :)
 
Last edited:
Code:
int pyraMonthsToGo() { return pyraMonthsToGo()+2; }

ED, I think I found a bug in your code. >_>

Did you find the easter egg? 2 + 2 months, quick mafs. Pyra CPU 's not hot. The ting goes speerrrahh, pap, pap.

Reversed: "pyraMonthsToGo" : Oh God, sh*t no more eep.


Algorithms in Javascript that do something:

So open a Firefox and type in the URL bar: javascript:alert(++[[]][+[]]+[+[]]);
Before hitting enter, guess what it does do....

Source: https://tutorialzine.com/2013/12/the-10-weirdest-programming-languages
 
So open a Firefox and type in the URL bar: javascript:alert(++[[]][+[]]+[+[]]);
Before hitting enter, guess what it does do....

Nothing, so far as I can tell. Does that mean I've got my browser security settings right?
 
it says "10" in the alert box for me. you can also paste it into the chrome console, if that's something you use.
 
here's a hash-table implementation which is O(1) delete with no negative repercussions (no "deleted" flags necessary in the table), and requires no probing across the table when looking for the right key.

i call it single-probe-via-salt hash table, or salty hash table for short. each time you insert an element, a salt is prepended to the key before hashing. the salt is chosen randomly, but such that the combined salt+key hashes to an open spot in the table. so there is some initial "probing" (by modifying the salt), but subsequent calls to the table require no probing, so long as you keep the salt alongside your key.

when expanding/shrinking the table, rehashing is required, and resalting, of course. none of your previous salts will be correct anymore. you might in fact want to keep a mapping<hash-table pointer, salts> alongside your key, in case the key is used in multiple salty hash tables. for best results, you can use a salty hash table to store the salts for your keys.
 
here's another nice and simple one:
Code:
// main.c
#include <stdio.h>
int main(int nargs, const char **vargs)
{   remove(vargs[0]);
    return 0;
}
compile with: "gcc main.c" and run with "./a.out". the second time you run it is when it does the magic.


speaking of code that deletes code, this is pretty funny:
https://github.com/munificent/vigil
Infinitely more important than mere syntax and semantics are [Vigil]'s addition of supreme moral vigilance. This is similar to contracts, but less legal and more medieval.
 
Last edited:
Back
Top