language-agnostic

Fastest way to modify one digit of an integer

Suppose I have an int x = 54897, old digit index (0 based), and the new value for that digit. What's the fastest way to get the new value? Example x = 54897 index = 3 value = 2 y = f(x, index, value) // => 54827 Edit: by fastest, I definitely mean faster performance. No string processing. ...

Code Golf: Playing Tetris

The basics: Consider the following tetrominoes and empty playing field: 0123456789 I O Z T L S J [ ] [ ] # ## ## ### # ## # [ ] # ## ## # # ## # ...

Efficient algorithm to find a point not touched by a set of rectangles

Input: a set of rectangles within the area (0, 0) to (1600, 1200). Output: a point which none of the rectangles contains. What's an efficient algorithm for this? The only two I can currently think of are: Create a 1600x1200 array of booleans. Iterate through the area of each rectangle, marking those bits as True. Iterate at the end a...

Model Profiles as Pattern Instances

Suppose you have a profile for an application. This profile stores some data specifically about one user. You can persist the details of the profile and pull them back each run. Does a profile make a good candidate for the State pattern? If not, is there a good heuristic to model this sort of functionality? ...

What is the safest way to run an executable on Linux?

I am trying to run a program compiled from C code from an unknown source. I want to make sure that the program does not harm my system in anyway. Like for instance, the program might have soemthing like system("rm -rf /") in the source, which is un-detectable, unless the code is thoroughly examined. I thought of the following 2 ways ...

Algorithm for maximizing coverage of rectangular area with scaling tiles

I have N scalable square tiles (buttons) that need to be placed inside of fixed sized rectangular surface (toolbox). I would like to present the buttons all at the same size. How could I solve for the optimal size of the tiles that would provide the largest area of the rectangular surface being covered by tiles. ...

2D coordinate normalization

I need to implement a function which normalizes coordinates. I define normalize as (please suggest a better term if Im wrong): Mapping entries of a data set from their natural range to values between 0 and 1. Now this was easy in one dimension: static List<float> Normalize(float[] nums) { float max = Max(nums); ...

When you update a form, would save and replace text, or avoid updating the text?

I have a form in jQuery where I update all the images via a simple ajax call to reload the entire page. Unfortunately the captions are included in that form as well, and as I start writing, my captions are overwritten. What then would you do to keep them? Would you.. Save them before the ajax call and replace them after it? Avoid up...

Data Repository Organization

So, I'm developing some software, and trying to keep myself using TDD and other best practices. I'm trying to write tests to define the classes and repository. Let's say I have the classes, Customer, Order, OrderLine. Now, do I create the Order class as something like abstract class Entity { int ID { get; set; } } class Order : ...

determine if all bytes are equal in a stream

i'm looking for an algorithm to check if all the bytes in a stream or buffer are equal. exist any algorithm to test that? I know wich this problem can be solved by walking across the stream and comparing each element with the first element, but I am looking for a better solution. the stream can have thousand of elements. ...

Functional Programming: Does a list only contain unique items?

I'm having an unsorted list and want to know, whether all items in it are unique. My naive approach would be val l = List(1,2,3,4,3) def isUniqueList(l: List[Int]) = (new HashSet()++l).size == l.size Basically, I'm checking whether a Set containing all elements of the list has the same size (since an item appearing twice in the original...

Is it good practice to throw an exception sooner than later?

Today I encountered the following situation: ("pseudo code") class MyClass { public void workOnArray(Object[] data) { for (Object item : data) { workOnItem(item); } } public void workOnItem(Object item) { if (item == null) throw new NullPointerException(); } } Now if the caller cal...

What should an escape character do if the following character doesn't need escaping?

An application I'm working on has a field where a string can be entered. Special characters in the string cause different things to be inserted when the string is evaluated, but these special characters can be preceded by an escape character (a backslash) which cause the special character to be output literally rather than its special m...

Adjust MIDI Note Volume

[I am doing this work in Java, but I think the question is language-agnostic.] I have a MIDI Note On volume (called "data2," it's 0-127) that I am adjusting with a fader (0 to 127). The "math" I am using is simple: newData2 = oldData2 * faderVolume / 127; Zero works perfectly, and 127 does too, but the volumes close to the bottom of ...

Algorithm for determining if 2 graphs are isomorphic

Disclaimer: I'm a total newbie at graph theory and I'm not sure if this belongs on SO, Math SE, etc. Given 2 adjacency matrices A and B, how can I determine if A and B are isomorphic. For example, A and B which are not isomorphic and C and D which are isomorphic. A = [ 0 1 0 0 1 1 B = [ 0 1 1 0 0 0 1 0 1 0 0 1 1 0 ...

Nested(deep) iteration for loops ? Good or Bad Practice?

Hi All, I wanted to ask, If writing nested(deep) iteration loops (as shown in example) a bad practice (in OOPs)? For example : If i want to zip all files that in all Workspaces. Given example classes are Workspace, Project , File please ignore avoid syntax errors for each Workspace workspace in WorkSpaces { Pro...

Is there any way to display a function call graph?

While browsing through source code in my IDE I'll sometimes wish I could see a call stack/s or function call graph from a particular point in the code (while the program isn't running) to help me understand the sequence of events better. An example of the functionality I'd like to see is: I click a function called 'sendNotificationEmai...

Burstable Output for Long running scripts

script <?php include('time.php'); //time script echo "First 100 users of SO<br/>"; for($i=0; $i<100;$i++){ $contents=file_get_contents("http://stackoverflow.com/privileges/user/".$i); preg_match('!<div class="summarycount al">(.+?)</div>!', $contents, $matches); $rep = $matches[1]; echo "<br/>".$i.") ".$rep."<br/>"; include('timetaken....

Trick Windows To See Second Monitor

Hi there, is there anyway to trick windows into thinking that a second monitor is connected to the computer, even if there isnt one? Example: Laptop also has an external screen connected into it. The external screen is the primary screen. When the screen is disconnected the laptop becomes primary even if the external screen is reconne...

When and How Does the Database Slow Down a Site?

I have read a book by O'Reilly that states that 70-80% of the performance of a site can be optimized via the front-end. That may mean that database queries may account to less than 20 or 30% of a site's performance. However, I have seen huge websites like Facebook and Twitter dedicate so much time to optimizing queries (via query caching...