algorithm

Algorithm for re-wrapping hard-wrapped text?

Let's say that I have written a custom e-mail management application for the company that I work for. It reads e-mails from the company's support account and stores cleaned-up, plain text versions of them in a database, doing other neat things like associating it with customer accounts and orders in the process. When an employee replies ...

Generating the partitions of a number

I needed an algorithm to generate all possible partitions of a positive number, and I came up with one (posted as an answer), but it's exponential time. The algorithm should return all the possible ways a number can be expressed as the sum of positive numbers less than or equal to itself. So for example for the number 5, the result woul...

How to elegantly compute the anagram signature of a word in ruby?

Arising out of this question, I'm looking for an elegant (ruby) way to compute the word signature suggested in this answer. The idea suggested is to sort the letters in the word, and also run length encode repeated letters. So, for example "mississippi" first becomes "iiiimppssss", and then could be further shortened by encoding as "4im...

Learning Algorithm and Saving Data in Software

I'm coming from a web-development background and I am wondering how I would make a learning algorithm in Java/C++. Not so much the algorithm part, but making the program "remember" what it learned from the previous day. I would think something like saving a file, but I suspect their might be an easier way. Apologies if this question is j...

Randomize a string in C

I'm trying to generate random permutations of an 80-character fixed string in C. Much to my dismay, the system I'm working on lacks strfry(). What's the best way for me to generate a random permutation of this string? Since this will be looped over approx. 100,000 times, performance is an issue. ...

algorithm to sum up a list of numbers for all combinations

I have a list of numbers and I want to add up all the different combinations. For example: number as 1,4,7 and 13 the output would be: 1+4=5 1+7=8 1+13=14 4+7=11 4+13=17 7+13=20 1+4+7=12 1+4+13=18 1+7+13=21 4+7+13=24 1+4+7+13=25 Is there a formula to calculate this with different numbers? ...

How do you like your primary keys?

In a fairly animated discussion in my team I was made to think what most people like as primary keys. We had the following groups- Int/ BigInt which autoincrement are good enough primary keys. There should be at least 3 columns that make up the primary key. Id, GUID and human readable row identifiers all should be treated differently. ...

Calculating optimal stock length

This is similar to the cutting stock problem, but with a slight difference. I want to find out what the optimal length of the stock is based on the sizes of the cuts. Potential complications: The Wikipedia article on the Cutting Stock Problem is way over my head. I suspect that understanding how to solve this problem might be critical...

Can quantum algorithms be used for encryption ?

Can quantum algorithms be useful? Has any one been successful in putting quantum algorithms to any use? ...

How to sort a string of characters in objective-C?

I'm looking for an Objective-C way of sorting characters in a string, as per the answer to this question. Ideally a function that takes an NSString and returns the sorted equivalent. Additionally I'd like to run length encode sequences of 3 or more repeats. So, for example "mississippi" first becomes "iiiimppssss", and then could be s...

Compression Algorithm for Encoding Word Lists

I'm am looking for specific suggestions or references to an algorithm and/or data structures for encoding a list of words into what would effectively would turn out to be a spell checking dictionary. The objectives of this scheme would result in a very high compression ratio of the raw word list into the encoded form. The only output req...

Calculating distance between zip codes in PHP

I grabbed a database of the zip codes and their langitudes/latitudes, etc from this This page. It has got the following fields: ZIP, LATITUDE, LONGITUDE, CITY, STATE, COUNTY, ZIP_CLASS The data was in a text file but I inserted it into a MySQL table. My question now is, how can i utilise the fields above to calculate the distance ...

ASP.NET VB - Some Maths with .NET

I need your help, For example I have a decimal type variable and I want to round up this way. Eg 3.0 = 3 3.1 = 4 3.2 = 4 3.3 = 4 3.4 = 4 3.5 = 4 3.6 = 4 3.7 = 4 3.8 = 4 3.9 = 4 4.0 = 4 4.1 = 5 4.2 = 5 etc.... How can I do that? ...

Finding the center of mass on a 2D bitmap

I'm coding a game, and I'd like to be able to find the center of mass of an arbitrary shape on a black and white bitmap such as this: 012345678 0.XX...... 1..XXX.... 2...XXX... 3..XXXXXXX 4...XXX... All "cells" have the same weight. Diagonally adjacent cells are not considered to be connected, and the shape will always be a single o...

Correct formulation of the A* algorithm

Hello, I'm looking at definitions of the A* path-finding algorithm, and it seems to be defined somewhat differently in different places. The difference is in the action performed when going through the successors of a node, and finding that a successor is on the closed list. One approach (suggested by Wikipedia, and this article) say...

Flex Matching Many Database Records (Quicksilver-like or Launchy-like matching)

Assume I have a database table with many names. I'd like to "flex match" against these names. I'm not sure if "flex match" is the proper term to use, but let's go with that for now. There have been similar discussions on "fuzzy matching," but I'm not really interested in phonetic matching. I'm interested in what I'd call ordered-subs...

Finding clusters of mass in a matrix/bitmap

This is in continuation with the question posted here: http://stackoverflow.com/questions/408358/finding-the-center-of-mass-on-a-2d-bitmap which talked about finding the center of mass in a boolean matrix, as the example was given. Suppose now we expand the matrix to this form: 0 1 2 3 4 5 6 7 8 9 1 . X X . . . . . . 2 . X X X . . X . ...

How do I multiply (and divide) BCD numbers by 10^x

I have a large (12 digit) BCD number, encoded in an array of 6 bytes - each nibble is one BCD digit. I need to multiply it by 10^x, where x can be positive or negative. I know it can be done by shifting left or right by nibble instead of bit, but it's a horrible implementation - especially in Javacard, which is what I'm using. Is there...

Algorithm to keep a list of percentages to add up to 100%

(code examples are python) Lets assume we have a list of percentages that add up to 100: mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] Some values of mylist may be changed, others must stay fixed. Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last three (35.0, 21.0, 23.0) may be changed. fix = mylist[:3] vari =...

Is there a proper algorithm to solve edge-removing problem?

There is a directed graph (not necessarily connected) of which one or more nodes are distinguished as sources. Any node accessible from any one of the sources is considered 'lit'. Now suppose one of the edges is removed. The problem is to determine the nodes that were previously lit and are not lit anymore. An analogy like city electric...