algorithm

Using Perl, how can I build a dynamic regexp by passing in an argument to a subroutine?

I would like to create subroutine with a dynamically created regxp. Here is what I have so far: #!/usr/bin/perl use strict; my $var = 1234567890; foreach (1 .. 9){ &theSub($_); } sub theSub { my $int = @_; my $var2 = $var =~ m/(??{$int})/; print "$var2\n"; } It looks like it will work, but it seems that once t...

What data structure will hold a bounded stack of items in LIFO ?

I'm looking for a data structure that is basically a bounded stack. If I declare that the stack can hold at most 3 items, and I push another item in, the oldest item is popped. ...

Fastest code C/C++ to select the median in a set of 27 floating point values

This is the well know select algorithm. see http://en.wikipedia.org/wiki/Selection_algorithm. I need it to find the median value of a set of 3x3x3 voxel values. Since the volume is made of a billion voxels and the algorithm is recursive, it better be a little bit fast. In general it can be expected that values are relatively close. Th...

Algorithm for Client-Server Games

For stand alone games, the basic game loop is (source: wikipedia) while( user doesn't exit ) check for user input run AI move enemies resolve collisions draw graphics play sounds end while But what if I develop client-server-like games, like Quake, Ragnarock, Trackmania, etc, What the Loop/Algorithm for client and the ser...

Graph navigation with C#

I having a bit of a quandry trying to come up with a good algorithm to navigate the following graph. If a user chooses "Table 21" as a starting point, I need to be able to get the path to any other table from that starting table. EX: If the user chooses "Table 21" as a start and then adds a value from "Table 8", I need to create the ...

Solving Nonograms (Picross)

Hey, it's Friday afternoon, let's have a fun puzzle/algorithm problem to solve. One of my favorite Nintendo DS games is Picross DS. The game is quite simple, it involves solving puzzles called Nonograms. You can try a simple online Picross clone here: TylerK's Picross. Nonograms are a grid, with sequences of numbers defined for every r...

Randomizing elements in an array?

I've created a site for an artist friend of mine, and she wants the layout to stay the same, but she also wants new paintings she'd produced to be mixed into the current layout. So I have 12 thumbnails (thumb1 - thumb12) on the main gallery page and 18 images (img1 - img18) to place. The approach I thought of was to create an array of ...

C#: Efficient algorithm to find greatest m elements in an N x N matrix

Hi, I would like to know if there's an efficient algorithm to find the greatest m elements in an N x N matrix, with a method header like this: double[] greatestValues(double[][] matrix, int numberOfElements); Any ideas? ...

Need an algorithm to solve this problem

Here are the constraints I have three buckets (water) capacities 10 litre, 2 liters and 50 litres. I will be given another bucket of water from 0 to 100 litres I have to fill buckets in order 10 first, 2 next and 50 last. Anything extra can be disposed. How best can this be done with the least lines of code? while loop? ideas please...

Implementing a 4-heap using an array.

What kind of math do you use to traverse the 4-heap when using an array to store all the elements? Specifically, how do you find the index of a parent node to a specific leaf? Let's say I have the following array: 0|1|2|3|4|5|6|7|8|9|10|... etc. with the heap then constructed from that with 1 being the root, 2..5 its children, 6..9 2'...

How do I Sort Collections in Objective-C?

I have an integer array in my Objective-C program. I'd like to sort it (ascending or descending, doesn't matter). In C++ I'd use the sort algorithm in the STL Algorithm library. How can I do this? ...

IT concepts that help/helped you in the "real" world.

Which IT concept (e.g. programming) you implement successfully outside the IT world? For example, normalizing databases are relevant to organizing my items. Understanding the real scale of information and search engine forces my to stop accumulating data (books, articles, pictures) and improve the method by which I find relevant info...

Backup Algorithm

I am looking at writing a program (.Net)for backing up files on a computer. How would I go on about knowing what files have changed to backup (I don't really want to scan the last modified dates each time. Are there any backup algorithms for backing up only the bits of a file have changed. What are the O notations for the algorithm? ...

A way to reverse queue using only two temporary queues and nothing more?

Is there a way to reverse items' order in queue using only two temporary queues (and no other variables, such as counters)? Only standard queue operation are available: ENQUEUE(e), DEQUEUE(), EMPTY()? Solutions in any language or pseudocode are welcome. ...

Print methods not called when running, called when debugging

Hi, I created a class with four methods. I inserted print statements into them, so I could see if they work properly. There is no problem with the first three. But when I call the fourth method nothing prints out. Strangely when I initiate debugger and move through the method step by step, the statements are called(and output printed). ...

Java task runtime

Hi! First of all I have to admit that these are very basic and primitive questions... I want to demonstrate different Algorithms in Java for sorting and searching, and to get a value for the runtime. There're issues I cannot solve: there's Hotspot compiling - which is a runtime optimization I need to deactivate (I guess). How do I ge...

Algorithm to recursivly go through a forest of children

Hey! Please let me know if this is bad practice or in someway a bad thing to do. The thing is in my program I need to make a method which goes through the root element and all child nodes of that element. My elements are like this: |--ID--|--Parent--|--Additinal info--| | 1 | 0 | root element | | 2 | 0 | root ele...

Fuzzy date algorithm

I'm looking for a fuzzy date algorithm. I just started writing one and realised what a tedious taks it is. It quickly degenerated into a lot of horrid code to cope with special cases like the difference between "yesterday", "last week" and "late last month" all of which can (in some cases) refer to the same day but are individually corre...

Proof: why does java.lang.String.hashCode()'s implementation match its documentation?

The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. The standard implementation of ...

Learning Algorithms: Never getting any better

I want to implement algorithms and data structures for practice. I have a bunch of textbooks I am working through and although I can understand the idea and theories I can never seem to close the book and implement working code on my own, like people do in ACM programming contests. I can learn how to write a linked list in C, and forget...