C# - There's no kill like overkill
First of all, dear GiMmEtHaCoDeZ, let's try to break down your task:
- Read the numbers
- Sort them
- Output the sorted numbers.
As "Divide and conquer" is very important strategy when working with software problems, lets tackle them one at a time
1. Reading
Another important issue in software is versatility. Since it's not specified how the user will input the numbers, that can happen via the console, via a file, via a web service, etc. Maybe even some method that we can't think of at the moment. So, it's important that our solution will be able to accommodate various types of input. The easiest way to achieve that will be to extract the important part to an interface, let's say
public interface IDoubleArrayReader
{
IEnumerable<double> GetDoubles();
DoubleArrayReaderType Type {get;}
}
where DoubleArrayReaderType
is an enumeration given with
public enum DoubleArrayReaderType
{
Console,
File,
Database,
Internet,
Cloud,
MockService
}
It's also important to make the software testable from the ground up, so an implementation of the interface will be
public class MockServiceDoubleArrayReader : IDoubleArrayReader
{
IEnumerable<double> IDoubleArrayReader.GetDoubles()
{
Random r = new Random();
for(int i =0; i<=10; i++)
{
yield return r.NextDouble();
}
}
DoubleArrayReaderType IDoubleArrayReader.Type
{
get
{
return DoubleArrayReaderType.MockService;
}
}
}
Next, the logical question is how we will know to load the appropriate IDoubleArrayReader
into the code. That's easy as long as we use a simple factory:
public static class DoubleArrayInputOutputFactory
{
private static Dictionary<DoubleArrayReaderType, IDoubleArrayReader> readers;
static DoubleArrayInputOutputFactory()
{
readers = new Dictionary<DoubleArrayReaderType, IDoubleArrayReader>();
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
try
{
var instance = Activator.CreateInstance(type);
if (instance is IDoubleArrayReader)
{
readers.Add((instance as IDoubleArrayReader).Type,
(instance as IDoubleArrayReader));
}
}
catch
{
continue;
}
}
}
public static IDoubleArrayReader CreateDoubleArrayReader(DoubleArrayReaderType type)
{
return readers[type];
}
}
Note that, we use reflection to load all active readers, so any future extensions will be automatically available
Now, in the main body of out code we just do:
IDoubleArrayReader reader = DoubleArrayInputOutputFactory
.CreateDoubleArrayReader(DoubleArrayReaderType.MockService);
var doubles = reader.GetDoubles();
2. Processing (sorting)
Now we need to process, i.e. sort the numbers we have acquired. Note that the steps are completely independent of each other, so to the sorting subsystem, it does not matter how the numbers were inputed. Additionally, the sorting behavior is also something that is subject to change, e.g. we might need to input a more efficient sorting algorithm in place. So, naturally, we'll extract the requested processing behaviour in an interface:
public interface IDoubleArrayProcessor
{
IEnumerable<double> ProcessDoubles(IEnumerable<double> input);
DoubleArrayProcessorType Type {get;}
}
public enum DoubleArrayProcessorType
{
Sorter,
Doubler,
Tripler,
Quadrupler,
Squarer
}
And the sorting behaviour will just implement the interface:
public class SorterDoubleArrayProcessor : IDoubleArrayProcessor
{
IEnumerable<double> IDoubleArrayProcessor.ProcessDoubles(IEnumerable<double> input)
{
var output = input.ToArray();
Array.Sort(output);
return output;
}
DoubleArrayProcessorType IDoubleArrayProcessor.Type
{
get
{
return DoubleArrayProcessorType.Sorter;
}
}
}
Of course, we will need a factory to load and manage the processing instances.
public static class DoubleArrayProcessorFactory
{
private static Dictionary<DoubleArrayProcessorType, IDoubleArrayProcessor> processors;
static DoubleArrayProcessorFactory()
{
processors = new Dictionary<DoubleArrayProcessorType, IDoubleArrayProcessor>();
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
try
{
var instance = Activator.CreateInstance(type);
if (instance is IDoubleArrayProcessor)
{
processors.Add((instance as IDoubleArrayProcessor).Type, (instance as IDoubleArrayProcessor));
}
}
catch
{
continue;
}
}
}
public static IDoubleArrayProcessor CreateDoubleArrayProcessor(DoubleArrayProcessorType type)
{
return processors[type];
}
}
3. Writing the output
Nothing much to say here, as this is a process that mirror the input. In fact, we could combine the reading and writing factories into a single DoubleArrayInputOutputFactory
, like this:
public interface IDoubleArrayWriter
{
void WriteDoublesArray(IEnumerable<double> doubles);
DoubleArrayWriterType Type {get;}
}
public enum DoubleArrayWriterType
{
Console,
File,
Internet,
Cloud,
MockService,
Database
}
public class ConsoleDoubleArrayWriter : IDoubleArrayWriter
{
void IDoubleArrayWriter.WriteDoublesArray(IEnumerable<double> doubles)
{
foreach(double @double in doubles)
{
Console.WriteLine(@double);
}
}
DoubleArrayWriterType IDoubleArrayWriter.Type
{
get
{
return DoubleArrayWriterType.Console;
}
}
}
public static class DoubleArrayInputOutputFactory
{
private static Dictionary<DoubleArrayReaderType, IDoubleArrayReader> readers;
private static Dictionary<DoubleArrayWriterType, IDoubleArrayWriter> writers;
static DoubleArrayInputOutputFactory()
{
readers = new Dictionary<DoubleArrayReaderType, IDoubleArrayReader>();
writers = new Dictionary<DoubleArrayWriterType, IDoubleArrayWriter>();
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
try
{
var instance = Activator.CreateInstance(type);
if (instance is IDoubleArrayReader)
{
readers.Add((instance as IDoubleArrayReader).Type, (instance as IDoubleArrayReader));
}
}
catch
{
continue;
}
}
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
try
{
var instance = Activator.CreateInstance(type);
if (instance is IDoubleArrayWriter)
{
writers.Add((instance as IDoubleArrayWriter).Type, (instance as IDoubleArrayWriter));
}
}
catch
{
continue;
}
}
}
public static IDoubleArrayReader CreateDoubleArrayReader(DoubleArrayReaderType type)
{
return readers[type];
}
public static IDoubleArrayWriter CreateDoubleArrayWriter(DoubleArrayWriterType type)
{
return writers[type];
}
}
Putting it all together
Finally, our main program will just use all this awesomeness we have already built, so the code will just be:
var doubles = reader.GetDoubles();
doubles = processor.ProcessDoubles(doubles);
writer.WriteDoublesArray(doubles);
where, e.g. we could define reader
, writer
and processor
using
IDoubleArrayReader reader = DoubleArrayInputOutputFactory.CreateDoubleArrayReader(DoubleArrayReaderType.MockService);
IDoubleArrayProcessor processor = DoubleArrayProcessorFactory.CreateDoubleArrayProcessor(DoubleArrayProcessorType.Sorter);
IDoubleArrayWriter writer = DoubleArrayInputOutputFactory.CreateDoubleArrayWriter(DoubleArrayWriterType.Console);
Are you also allowed to use other languages than C? – ProgramFOX – 2013-12-27T09:25:29.977
Perhaps. I really want to avoid a case where just the choosen language is enough reason to troll the lazy OP. Otherwise, you could just plainly encode the correct solution using brainfuck, golfscript or some crazy language, but this would ruin the intention of this as everybody would do that. But if you use, lets say Java, C++ or Ruby, it would be ok. Further, in most homework questions the OP says what language he wants. – Victor Stafusa – 2013-12-27T09:33:10.180
OK, thanks. I just wanted to know whether I could use C#. – ProgramFOX – 2013-12-27T09:34:30.593
1“Crazy language”? I think the expression you are looking for is esoteric language. – manatwork – 2013-12-27T09:39:37.133
@ProgramFOX I edited the question to clarify your point. – Victor Stafusa – 2013-12-27T09:40:23.157
1@manatwork Not exactly. Although this englobes all (or almost all) esoteric languages, there might still be non-esoteric languages that are too crazy for that, e.g. no one would think in using Logo for this, except if trolling. Anyway, I let that be a bit subjective. The chosen language should not be the sole reason of the trolling, and people are discouraging to upvote answers that uncreatively abuses the language choice. If you are intending in abusing the choosen language, be really creative about that. – Victor Stafusa – 2013-12-27T09:53:35.180
48Stack Oversort – ThiefMaster – 2013-12-27T13:26:31.133
hmm... using code from [this question](Cardinal Numbers in Standard American English) and sorting the result? – SeanC – 2013-12-27T14:26:04.310
1This is going to be a great tag. Had to laugh at quite a few posts here, but I think it isn't going to attract a whole lot of OP's. Anyway, nice :) – tomsmeding – 2013-12-27T14:52:53.503
1
This reminds me of something we did for someone suspected of using us for a homework problem: Could U do it better?.
– IQAndreas – 2013-12-27T15:00:24.087Isn't better to just post the link to good resource, where he/she could learn something ? Instead wasting his/her time and trolling ? – Paul Brewczynski – 2013-12-27T15:47:51.737
6@bluesm If someone has already decided to ask someone else to solve their problem instead of "wasting" their own time learning, posting a link to where they can learn on their own isn't going to do any good. – IQAndreas – 2013-12-27T16:21:21.380
"The idea of this category is..." - it seems like this belong on the tag wiki, not in the question itself. – Dukeling – 2013-12-27T17:12:06.417
2@Dukeling Yes, this is true. But since this is the first question in the category, I added the description here to introduce it. Further questions could simply reference the category and go to what the lazy OP asks. – Victor Stafusa – 2013-12-27T17:14:09.013
That does make sense, but the problem is that, if someone who already knows about this (presumably from other, newer questions) comes across this question, about 90% of the question would be unnecessary. Given that [se] questions are supposed to be in it for the long run, I don't think it should be here. Rather in the tag wiki with an associated Meta post, if any discussion is desired. But anyway, that's just my opinion. – Dukeling – 2013-12-27T17:30:21.140
I believe the category would be better if the rules stated that the program should seem ok to the lazy student, but absolutely not to their professor. Otherwise we will get
print [the output requirement from spec]
in every question at least once. – shiona – 2013-12-27T18:15:29.483I suggested an edit to the tag wiki of code-trolling. Not sure if we should edit the description out; I'll leave that up to the OP/other users – Doorknob – 2013-12-27T18:18:06.590
1
I opened a meta discussion for the rules, hoping if will be of help: http://meta.codegolf.stackexchange.com/questions/746/code-troll-rules
– shiona – 2013-12-27T19:47:57.1232silly, what is the point? to humiliate and discourage? i get that sometimes people are just trying to get their homework done, but really its easier just to say so and point them in the right direction. – Matt Evans – 2013-12-27T21:31:44.760
1At this rate, you might get the second ever Great Question badge on this site :-D – Doorknob – 2013-12-27T22:15:34.040
Given the limited state of a lot of PRNGs, I wonder how many of these bogosorts are actually capable of terminating at all on non-trivial input. – sh1 – 2013-12-28T00:57:33.030
3Wow, this question's about to get 100 upvotes and 10,000 views in less than 24 hours! – Joe Z. – 2013-12-28T01:37:24.733
2@JoeZ. Yeah, I could not even dream about that before. Totally awesome. I am very pround of this and would like to thank everybody. – Victor Stafusa – 2013-12-28T01:41:12.680
2
So, @Victor, I decided to continue the trend, to help expand the category.
– Joe Z. – 2013-12-28T01:53:33.8431@JoeZ. You were right. It got 100 upvotes and 10,000 views in less than 24 hours (it took some 18 or 19 hours)! Further it already has 80 answers. Got two gold badges! – Victor Stafusa – 2013-12-28T02:40:59.737
18My goodness, Victor, your About box is so sad... we all have our ups and downs but you shouldn't beat yourself up man. You're a hero for Code Golfers everywhere now! – SimonT – 2013-12-28T04:21:35.767
2This is definitely the first question I have ever seen that has more than 100 answers.... Is this good or bad? – Justin – 2013-12-28T09:48:23.580
4
I'm surprised no one has offered a solution based on sleep sort yet
– Frank Farmer – 2013-12-28T11:02:54.4102It's not "give-me-dah-codez", it's "plzsendtehcodz"! – The Guy with The Hat – 2013-12-28T13:04:59.637
1There should be some (manually awarded) badge for giving this type of answer - like
Sheldon Cooper
badge. – Konrad Morawski – 2013-12-28T15:55:35.620Wow, there are 6 questions from this site on the hot questions list, and 5 of them are [tag:code-trolling] – Justin – 2013-12-28T18:17:24.503
@Quin Ahahaha, not sure if that's great or... XD – Doorknob – 2013-12-28T18:51:20.580
1
This is like Photoshop Trolling. Warning: hilarious.
– Chloe – 2013-12-28T19:21:55.303Explanation of a new class of questions should have gone on meta first and then migrated to the tag wiki once something was settled. – dmckee --- ex-moderator kitten – 2013-12-28T20:45:22.050
I found that it's very strange that on one mentioned Shell Scripting before, so I posted my answer which, in a nutshell, is a call to the
sort -n
command. – Damkerng T. – 2013-12-28T22:31:13.887Not worth the effort to code it so I'm not entering, but my suggestion for an easy-to-understand pessimized sort routine would be to random shuffle and then check whether they're ordered yet. – keshlam – 2013-12-29T21:47:20.690
@keshlam, that's bogosort, and there are several of them posted already. However, I assert that my entry is less efficient than bogosort for array lengths less than about 20. – sh1 – 2013-12-30T01:35:17.577
Too short for an answer, here is T-SQL version (minus CR-LF): create type dbo.ArrayOfDouble as table ( Item float(53) ) go
create function SortArrayOfDouble ( @array ArrayOfDouble readonly ) returns table as return ( select Item from @array order by Item ) go – Pieter Geerkens – 2014-01-01T00:14:29.280
1Wow, this now has 2^8 upvotes! – Cruncher – 2014-01-16T18:21:33.197
Code-trolling is in the process of being removed, as per the official stance. This question has historical value for obvious reasons, so it is being kept around, but I am locking it now.
– Doorknob – 2014-05-10T16:08:26.877