11 ms·
Trolling homework questions - sorting
- ultimatedelman 13y agoThis is brilliant because it encourages thinking a little differently to come up with the wrong answer (which helps you learn to identify wrong code). Nice work OP
- zhemao 13y agoWell, a lot of them aren't wrong answers, just really inefficient or overly-complicated implementations.
- mjcohen 13y ago1. Generate all permutations of the input. 2. Return the permutation that is in order. 3. Profit!
- shawabawa3 13y ago1. Output all permutations of the input 2. There is no step 2. You've technically outputted the input list in order...somewhere
- yeukhon 13y agoThe ultimate answer is to ask your young siblings to sort them for you. The price is a little time and some candy. Or since so many people are unemployed, ask them to crowdsource and work for something in return! Or just ask wolframalpha.
- HeyImAlex 13y agoSet up a job through the Amazon mechanical Turk API and that might actually be possible to do programmatically.
- cromulent 13y agoThat was my first thought - use a call to the MT api to get the sort done, and then "wait for response". Meets the requirements of the competition perfectly.
- knieveltech 13y ago"trollsort.c:43:3: warning: incompatible implicit declaration of built-in function ‘printf’" Epic. Epic I say.
- aembke 13y agoWow. http://codegolf.stackexchange.com/a/16274 http://codegolf.stackexchange.com/a/16274
- yeukhon 13y agoNot as impressive as this http://www.jsfuck.com/ http://www.jsfuck.com/
- ultimatedelman 13y agoyeah, that's why i don't code in perl
- NegativeK 13y agoClaiming that Perl is a bad language because you can obfuscate it just means that you've never looked for "obfuscated <better language>".
- lmm 13y agoI've looked quite hard for obfuscated Python (and there are a couple of other answers to this question using it). It's always been much more readable than that perl.
- SwellJoe 13y agoAnd, it's one of many reasons I do.
- Moto7451 13y agoYour everyday Perl is much nicer: perl -e 'my $vals = ".003, .002, .005"; my @sorted = sort { $a > $b } map { $_ =~ s/ +//g; $_ } split(",",$vals); print "$_ " foreach @sorted;' You can alias the topic variable ($_) if you're not a fan.
- jeremymcanally 13y agoNo one suggested sleep sort? That's the ultimate trolling-but-almost-legitimate sorting algorithm. :)
- mihai_ionic 13y agoAnd it even has the best time bound you can get for a comparison based sort. Most OS schedulers would use a heap/priority queue internally for timers, which makes sleep sort O(n log n).
- gizmo686 13y agowouldn't sleep sort be O(k), where k is the largest element to be sorted? (Assuming the clock and scheduler are precise enough to achieve the correct answer the first attempt).
- mihai_ionic 13y agoI would't really count time passing as an operation, since other programs are able to run during that time.
- gweinberg 13y agoYou could keep it linear by first going through the list to find the max, then scaling down.
- Moto7451 13y agoI noticed this myself and decided to write a glorious example in Objective C: http://codegolf.stackexchange.com/a/16390/11612 http://codegolf.stackexchange.com/a/16390/11612
- justinhj 13y agoI wrote a version in Go http://codegolf.stackexchange.com/a/16468/11452 http://codegolf.stackexchange.com/a/16468/11452
- 13y ago
- Gwen_Bell 13y agoThis reminds me of learning Git. It was hard. Probably the hardest thing I've ever learned. But it was worth it, because I Did The Work, and now I'm a hacker.
- codezero 13y agoTrolling people looking for easy answers is pointless. Folks like this won't use a single source to send in their work. The problem with this approach is you often penalize people who are truly curious and not trolls. If you were to allow the masses to determine whether a question is a troll question or not, using an OR, allowing anyone to flip the bit, then almost every question would be considered a troll question. You get a lot of false positives. Providing a thorough, quality answer to a troll question won't help advance that troll's career, they will pay the price later, for sure, when their intellect is on the line. Providing a trolly answer, and treating the question asker as insincere will inevitably treat a sincere person like shit, and turn them off entirely from entire genres and disciplines. Treat resources as sincere, and those who wish to abuse them for temporary short term gain will, and I am very confident about this, end up fucking themselves over. *If you downvote this, please let me know what you disagree with, I'm open to debate.
- yeukhon 13y agoI didn't downvote you. Two thoughts: 1. I read the whole thing three times and I still don't really understand what your objective is.... and do you actually know the purpose of the trolling thread or not. The purpose is to have fun and pretend one as a student who is under some stress and is trying to figure out some funny yet creative ways to brain fuck with the TA or professor. Even if it receives F, whatever. > Treat resources as sincere, and those who wish to abuse them for temporary short term gain will, and I am very confident about this, end up fucking themselves over. Well, most of the questions we ask on the Internet are really for short-term gain anyway. We hope someone could tell us why the fuck the X.so file is not linked and we move on afterward. Most of the answers are actually not some quick jerky answers. people actually took time to write them. And in the process of writing an answer, the author learns. Ever heard of "one teaches, two learns?" 2. Why so serious? The Internet is full of shit half of the time. Wasting 100MB in the database for 100 trolling answers isn't going to kill some hungry child in Africa.
- codezero 13y agoI'm probably so serious because it seems like so many other people are so dismissive :)
- applecore 13y agoIf you don't want to answer the question, don't answer the question.
- chronic25 13y agoSays who? I'll give a wrong answer if I so please.
- ChuckMcM 13y agoSigh, don't have an SO account but if I did, I would suggest this code which I call randsort -- #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i, ndx; double my_numbers[10]; double sorted_numbers[10]; uint16_t picked; for (i = 1; i < argc; i++) { my_numbers[i-1] = atof(argv[i]); } while (1) { picked = 0; ndx = 0; /* sort the numbers */ while (picked != 0x3ff) { i = (rand() / 100) % 10; if ((picked & (1 << i)) == 0) { picked |= (1 << i); sorted_numbers[ndx++] = my_numbers[i]; } } /* verify they sorted correctly */ for (i = 8; i >= 0; i--) { if (( i == -1) || (sorted_numbers[i] > sorted_numbers[i+1])) { break; } } if (i == -1) { printf(" Sorted: \n"); for (i = 0; i < 10; i++) { printf(" %d: %f\n", i, sorted_numbers[i]); } exit(0); } } }
- demallien 13y agoNice. Is that O(e^n)? Out of curiosity, why do you divide the rand() by 100 before applying the mod?
- ChuckMcM 13y agoNo reason on the divide. Just because. That is probably a reasonable guess on the big O value. Clearly O(rand) is cheeky but in accurate. And the PRNG will walk the number space so it will eventually succeed, but computing how long it will take for 'n' numbers eludes my math reasoning skills.
- emilv 13y ago> the PRNG will walk the number space Are you sure? What if there's an infinite amount of tries to get to a certain number? You can of course reason about the average case, but maybe the worst case (when there's an input number which is never found by the PRNG) does never halt. > O(rand) For sorting algorithms we usually compare in the number of input elements. You can reason about the average case where the numbers are found in average time so you can consider the number of iterations to find the correct number a constant (a very large constant but a constant nonetheless).
- Houshalter 13y agoI considered considering this ridiculously over-complicated approximate solution. Train a neural network to sort with a bunch of examples of sorted arrays.
- tlarkworthy 13y agoI would be interested if you could expand on how you would alter the NN architecture for different sized lists
- Houshalter 13y agoYou would have to deal with a maximum size limit, or sort only sections of it at a time. I mean it might technically be possible if you did some kind of recurrent neural network and gave the inputs through time rather than through space. And you would also have to let it interact with some kind of external memory or workspace. Perhaps you could train it to just perform operations on the list itself (compare two elements, swap two elements, etc.) That would be the simplest way.
- emilv 13y agoA maximum size limit is an obvious troll to get down to O(1) sort!
- raverbashing 13y agoMy favourite way goes like this https://gist.github.com/anonymous/8158332 https://gist.github.com/anonymous/8158332 Basically, each thread sleeps for the time corresponding to the number
- donquichotte 13y agoHaha, fantastic! This is an implementation in go: https://gist.github.com/anonymous/8158822 https://gist.github.com/anonymous/8158822
- xzel 13y agoEven help on the internet is cynical now? Honestly, interesting idea though.