Interesting/humorous algorithms

Discussion in 'Offtopic Discussions' started by ible, Jan 26, 2018.

  1. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    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: Jan 26, 2018
    Tags:
    FBnil likes this.
  2. Caine

    Caine Hardcore Member

    Joined:
    Jun 5, 2008
    Messages:
    4,081
    Location:
    Netherlands
    Obligatory xkcd 221:
    Code:
    int getRandomNumber()
    {
       return 4; // chosen by fair dice roll.
                 // guaranteed to be random.
    }
    
     
  3. JDTAY

    JDTAY Half Pepperoni, All Cheese

    Joined:
    Sep 15, 2015
    Messages:
    419
    Location:
    North Carolina, USA
    Code:
    int pyraMonthsToGo() { return pyraMonthsToGo()+2; }
    ED, I think I found a bug in your code. >_>
     
    Yorizuka, gpb and levi like this.
  4. Binky

    Binky Death's Steed Staff Member

    Joined:
    May 28, 2003
    Messages:
    6,803
    Location:
    16A (TO)
    WPEG is still my favourite. The only lossless compression algorithm where the size of the output is completely independent of the input size/complexity!

    http://gkoberger.github.io/stacksort/ is good, too
     
    pmprog and FBnil like this.
  5. FBnil

    FBnil What is laugh? Baby don't hurt me....

    Joined:
    Dec 14, 2012
    Messages:
    2,364
    Location:
    Yurp
    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
     
  6. Caine

    Caine Hardcore Member

    Joined:
    Jun 5, 2008
    Messages:
    4,081
    Location:
    Netherlands
    That wtfjs site linked in that article is hilarious.
     
  7. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
  8. Binky

    Binky Death's Steed Staff Member

    Joined:
    May 28, 2003
    Messages:
    6,803
    Location:
    16A (TO)
    Nothing, so far as I can tell. Does that mean I've got my browser security settings right?
     
    FBnil likes this.
  9. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    it says "10" in the alert box for me. you can also paste it into the chrome console, if that's something you use.
     
  10. JDTAY

    JDTAY Half Pepperoni, All Cheese

    Joined:
    Sep 15, 2015
    Messages:
    419
    Location:
    North Carolina, USA
  11. JDTAY

    JDTAY Half Pepperoni, All Cheese

    Joined:
    Sep 15, 2015
    Messages:
    419
    Location:
    North Carolina, USA
  12. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    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.
     
  13. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    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
     
    Last edited: Feb 16, 2018
    levi likes this.
  14. FBnil

    FBnil What is laugh? Baby don't hurt me....

    Joined:
    Dec 14, 2012
    Messages:
    2,364
    Location:
    Yurp
    @ible actually, the first time it was already gone...
     
  15. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    FBnil likes this.
  16. ible

    ible Advanced Guard Tower

    Joined:
    Mar 24, 2014
    Messages:
    2,047
    Location:
    Thrice in CA, twice in ND, once in the NL
    FBnil likes this.
  17. FBnil

    FBnil What is laugh? Baby don't hurt me....

    Joined:
    Dec 14, 2012
    Messages:
    2,364
    Location:
    Yurp
    @ible wow, it took me a while to understand it.
     

Share This Page

Loading...