language-agnostic

Adjacent number algorithm grouper

By which I mean this: Given the input set of numbers: 1,2,3,4,5 becomes "1-5". 1,2,3,5,7,9,10,11,12,14 becomes "1-3, 5, 7, 9-12, 14" This is the best I managed to come up with: [C#] Which feels a little sloppy to me, so the question is, is there somehow more readable and/or elegant solution to this? public static string[] FormatIn...

When is Object Oriented not the correct solution?

I've encountered lately some opinions saying that Object Oriented design/programming should not always be used. Do you know some use-cases that will not benefit from and should not use Object Oriented design? For example: there are some problems (concerns) that will benefit from AOP. ...

Algorithm to find similar text

I have many articles in a database (with title,text), I'm looking for an algorithm to find the X most similar articles, something like Stack Overflow's "Related Questions" when you ask a question. I tried googling for this but only found pages about other "similar text" issues, something like comparing every article with all the others...

Using the main method of classes for debugging?

It is good practice to use the main method to test a java/.net class? I've seen it reccommended in some text books, but to me it seems like using a unit testing framework would make more sense... The main method gives you one point of entry to the class and you can test one aspect of the classes functionality. You could I guess test ma...

Pattern name for flippable data structure?

I'm trying to think of a naming convention that accurately conveys what's going on within a class I'm designing. On a secondary note, I'm trying to decide between two almost-equivalent user APIs. Here's the situation: I'm building a scientific application, where one of the central data structures has three phases: 1) accumulation, 2) a...

How to Calculate Recurring Digits?

Given two integers a and b, how would I go about calculating the repeating decimal of a / b? This can be in any language; whatever it's easiest for you to express it in. ...

Binary Search in Array

How would I implement a binary search using just an array? ...

How does XOR variable swapping work?

Can someone explain to me how XOR swapping of two variables with no temp variable works? void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } I understand WHAT it does, but can someone walk me through the logic of how it works? ...

Best practices: Many small functions/methods, or bigger functions with logical process components inline?

Is it better to write many small methods (or functions), or to simply write the logic/code of those small processes right into the place where you would have called the small method? What about breaking off code into a small function even if for the time being it is only called from one spot? If one's choice depends on some criteria, w...

Can source code examples be kept in a SQL database while retaining all formatting? If so...

Can source code examples be kept in a SQL database while retaining all formatting (tabs, newlines, etc.)? If so what data type would be used? ...

one pretty printer "to rule them all"

Hi, I'm looking for a tool that can pretty-print (AKA tidy or beautify) source code in as many languages as possible. Those I'm particularly keen on include: Java JSP HTML JavaScript SQL JSON XML Ideally, the tool should be able to update source files in-place and be able to format more than a single file at-a-time. It would be ...

Best way to format if statement with multiple conditions.

If you want to some code to execute based on two or more conditions which is the best way to format that if statement ? first example:- if(ConditionOne && ConditionTwo && ConditionThree) { Code to execute } Second example:- if(ConditionOne) { if(ConditionTwo ) { if(ConditionThree) { Code to execute } ...

Exceptions or error codes

Yesterday I was having a heated debate with a coworker on what would be the preferred error reporting method. Mainly we were discussing the usage of exceptions or error codes for reporting errors between application layers or modules. What rules do you use to decide if you throw exceptions or return error codes for error reporting? ...

When evaluating a design, how do you evaluate complexity?

We all know to keep it simple, right? I've seen complexity being measured as the number of interactions between systems, and I guess that's a very good place to start. Aside from gut feel though, what other (preferably more objective) methods can be used to determine the level of complexity of a particular design or piece of software? ...

Would it be bad form to put braces on the same line as the statement for single line "if" statements?

So I know it's considered somewhat good practice to always include curly braces for if, for, etc even though they're optional if there is only one following statement, for the reason that it's easier to accidentally do something like: if(something == true) DoSomething(); DoSomethingElse(); when quickly editing code if you don'...

fast geometric proximity predicate

I have 3 points (A, B and X) and a distance (d). I need to make a function that tests if point X is closer than distance d to any point on the line segment AB. The question is firstly, is my solution correct and then to come up with a better (faster) solution. My first pass is as follows AX = X-A BX = X-B AB = A-B // closer than...

Any sensible examples of creating inheritance without creating subtyping relations?

I've been teaching OOP and was trying to convey to my students the important difference between inheritance and the creation of a subtype relation between two types. For example, in C++, I could use private inheritance to ensure that nobody outside sees the subtyping relation. However, while I can think of a lot of situations where I wo...

multiplication chains that result in a constant modulo a power of 2

Is there a practical algorithm that gives "multiplication chains" To clarify, the goal is to produce a multiplication change of an arbitrary and exact length Multiplication chains of length 1 are trivial. A "multiplication chain" would be defined as 2 numbers, {start} and {multiplier}, used in code: Given a pointer to array of size ...

Where can I find a good documentation about the core concept of REST?

I found a good description on wikipedia with a few reference links, but there may be better ones, please help me to find these! To be clear, I am not looking for implementation for any language specific implementation guides, just the pure concept. ...

Testing with random inputs best practices

NOTE: I mention the next couple of paragraphs as background. If you just want a TL;DR, feel free to skip down to the numbered questions as they are only indirectly related to this info. I'm currently writing a python script that does some stuff with POSIX dates (among other things). Unit testing these seems a little bit difficult tho...