language-agnostic

Find the next number in a sequence

This has been a programming problem which has interested me for a while. You give the program a squence of numbers seperated by commas. It then finds a suitable formula from these numbers to find the next number in the sequence. In: 1,2,3,4,5 Out: 6 In: 2,4,6,8 Out: 10 In: 10,8,6,4 Out: 2 I'm not sure if this problem is much more c...

Algorithm to locate local maxima

I have data that always looks something like this: I need an algorithm to locate the three peaks. The x-axis is actually a camera position and the y-axis is a measure of image focus/contrast at that position. There are features at three different distances that can be in focus and I need to determine the x-values for these three poi...

Selecting an optimum set according to ranked criteria

I am given a string, and a set of rules which select valid substrings by a process which isn't important here. Given an enumeration of all valid substrings, I have to find the optimum set of substrings according to a set of ranked criteria, such as: Substrings may not overlap All characters must be part of a substring if possible Use a...

Toy projects for new programers

When I was first started teaching myself programming, after finishing a tutorial I would feel like I still couldn't do anything in the language. So, I looked around to find something to work on. Since I had just learned a few of the basics, the amount of work involved in finding, reading and adding to an open source project seemed insurm...

How do I send a RST instead of a normal close, for testing?

I have some code that seems to not handle it well when a TCP connection is closed via the RST flag instead of a normal handshake for closing the connection. The "connection reset by peer" situation. I'd like to write a TCP server that always closes via RST so that I can reproduce the bug and write some unit tests for this. So... How do ...

Is this how rotation about a point is done?

Let's say I have a polygon with points: (0,0) (100,0) (100,100) (0,100) Lets also let it's center be (50,50). To rotate it would I add 50 to each component of each point, then do x' = cos(theta)*x - sin(theta)*y y' = sin(theta)*x + cos(theta)*y Then subtract 50 from each component of each point? Thanks ...

Scaling a polygon toward an edge?

I know that to scale verticies I simply have to multiply by a scale factor. But I noticed that most vector drawing applications show the shapes bounding box and by dragging one of the edge it will scale the geometry toward the opposite edge, then if you go past this edge it will end up mirroring the geometry on that axis. How is scaling ...

How does this integer encoding work?

In this code golf question, there is a python answer that encodes the lengths of all integers from 1 to 99 in english to a big number: 7886778663788677866389978897746775667552677566755267756675527886778663788677866355644553301220112001 To get the length of n, you just have to calculate 3 + (the_big_number / (10**n)) % 10. How does thi...

Which direction is better for sorting multi-column lists?

Please note, I'm not asking how to implement or code a multi-column list. There are two ways to sort multi-column lists: horizontal and vertical. Many of the Microsoft System.Web.UI.WebControls have a RepeatDirection property that offers these two options. I'm sure other frameworks also offer these direction options. Below are sampl...

Why does a file checked into CVS become double-spaced?

Problem Frequently (but not every time) when using CVS to check in files like: .java, .cs, .xml, etc, every line of the file is gets a carriage return. Example: File before check-in by a team member: // Begin file class Foo { public Foo() { // Do step 1 // Do step 2 } } // End file Fil...

Hg: Update on a line-by-line level?

I have a bug that is present in one changeset but not its parent. Is there some functionality in mercurial where I can "update" in smaller increments, to see where the problem starts? For example, if the diff is a change in functions A, B, and C, I would run the test suite after making each of those changes, to try to diagnose the probl...

Object-oriented Paradigm Question...

Even though I've been programming for quite a while now, when it comes to coupling objects I always seem to bang my head against the wall so I'm wondering if anyone has any resources or golden rules I can follow. Let me give a small example, in no particular language... class Person { private int personnel_id private String fir...

How would you generate a key that is only valid for three months?

Hi, I am wondering if it is possible to generate a "key" that is valid for a period of (approximately) three months? For example, let's say (hypothetically) that I generate a key like this (pseudocode): Key = HASH ( MachineID, Salt ); And the way I verify a key is valid is to check like this: isValid(Key) { return Key == HASH ( ...

Print any document with a textual identifier on it

Is there any possibility of printing any document (e.g. image, PDF, Office document, etc) with a text label at the top of page? Modifying actual files isn't an option for me. I'm wondering if there's anything like that provided in Windows printing system. Thanks. ...

What is a delegate?

I was coding some stuff on objetive c.. but I still dont get it, I dont know/understand what a delegate is at all. Maybe cuz my main programming language is C++ and Java... dont know. I searched the web looking for an ENGLISH explanation, but, seems like I dont speak english :) ...

Advices to manage my own code toolbox

On every new professional project I tend to (re)-create the same helpers and utils classes. It is not wheel re-invention, but rather some small tools that can fit in any project. I wish I can keep them in a 'code toolbox' that I can re-use on each time. But I'm facing some problems (Legal question, version problem, dependencies, ...). ...

OK, so I'm not a beginner anymore. What comes next?

In a certain sense, I'm still very much a beginner, but I don't need to read C++ Primer or Learn C++ in 21 Days, or at least, I only take a peek every now and then for reference's sake. So what is my problem? Basically, I started to write a small game to help my process of learning. Along the way, I've learned most of anything that you ...

Interfaces vs Public Class Members

I've noticed that some programmers like to make interfaces for just about all their classes. I like interfaces for certain things (such as checking if an object supports a certain behavior and then having an interface for that behavior) but overuse of interfaces can sometimes bloat the code. When I declare methods or properties as public...

One big checkin or several smaller ones?

Yesterday when i checked out the latest version of our internal tool i saw about 30+ new versions. This got me curious since i thought that somebody finally fixed those annoying bugs and added that feature i was waiting for so long... and guess what? None of this happened, someone just thought it would be nice to update some headers and ...

Why const parameters are not allowed in C#

It looks strange especially for C++ developers. In C++ we used to mark parameter as const in order to be sure that its state will not be changed in method. There are also other C++ specific reasons also like passing const ref in order to pass by ref and be sure that state will not be changed. But why we can't mark as const method paramet...