algorithm

Next month, same day in PHP

I got a last time (and urgent) requeriment of an "event" that needs to be scheduled the same day of every month. Say of you set the start date on the 1st May you should get the next events on the 1st of Jun, 1 Jul etc. The problem comes with a start date on the 31st (the next ones could be 30 or 28 depending on the month) Considering ...

Find all possible factors in KenKen puzzle 'multiply' domain

A KenKen puzzle is a Latin square divided into edge-connected domains: a single cell, two adjacent cells within the same row or column, three cells arranged in a row or in an ell, etc. Each domain has a label which gives a target number and a single arithmetic operation (+-*/) which is to be applied to the numbers in the cells of the do...

Finding bugs by bisecting (searching) revision history, and untestable commits (revisions)

Most modern version control tools have a command to find a change that introduced a bug by binary search (bisecting) of a history. Such command might be built-in, or it might be provided as extension or plugin. Examples include git-bisect in Git, "hg bisect" in Mercurial (earlier available as hbisect extension), and bzr-bisect plugin for...

Way to encrypt a single int

How can you inexpensively two-way encrypt a 32 bit int, such that every number maps to some other int in that space and back in a way that's difficult to predict? And doesn't require pre-storing 4.29 billion ints in a mapping table, of course. ...

How to generate permutations of a list without "reverse duplicates" in Python using generators

This is related to question How to generate all permutations of a list in Python How to generate all permutations that match following criteria: if two permutations are reverse of each other (i.e. [1,2,3,4] and [4,3,2,1]), they are considered equal and only one of them should be in final result. Example: permutations_without_duplicate...

Trie (Prefix Tree) in Python

I don't know if this is the place to ask about algorithms. But let's see if I get any answers ... :) If anything is unclear I'm very happy to clarify things. I just implemented a Trie in python. However, one bit seemed to be more complicated than it ought to (as someone who loves simplicity). Perhaps someone has had a similar problem? ...

Which sorting algorithm is used in stl and .net base library default search?

I am now working on an imprived version of merge sort. I implemented it with C++ and C#. Then compared them with the stl sort and array.sort() algorithm respectively. In C++ I have got an equal (sometimes better) result. But in C#, I had to use unsafe code for using pointers. Here the performence is not that much comparable with default ...

How many digits will be after converting from one numeral system to another

The main question: How many digits? Let me explain. I have a number in binary system: 11000000 and in decimal is 192. After converting to decimal, how many digits it will have (in dicimal)? In my example, it's 3 digits. But, it isn't a problem. I've searched over internet and found one algorithm for integral part and one for fractional...

Sorting a string that could contain either a time or distance

I have implemented a sorting algorithm for a custom string that represents either time or distance data for track & field events. Below is the format '10:03.00 - Either ten minutes and three seconds or 10 feet, three inches The result of the sort is that for field events, the longest throw or jump would be the first element while for ...

Decomposing a 3d mesh into a 2d net

Suppose you have a 3 dimensional object, represented as a 3d mesh in some common file format. How would you devise an algorithm to decompose the mesh into one or more 2d 'nets' - that is, a 2-dimensional representation that can be cut out and folded to create the original 3d object. Amongst other things, the algorithm would need to acco...

Index Fabric (layered Patricia trie)

I'm currently trying to implement the Index Fabric for a dna sequence data search system: Index fabric algorithm I could implement the normal patricia trie, but I still couldn't understand how to add layers. I also tried google but couldn't find enough information about adding layers to the patricia trie there either. In the paper me...

Is there an efficient implementation of tetration?

After recently answering a question involving the Ackerman function part of which involved a function for computing the tetration of a number. Which led me to ponder if there was a more efficient way to do it. I did some testing on my own but I'm limited mainly by the fact that even a number such as 5^^3=5^3125 given 5^3 is roughly 10^2...

I need a fast 96-bit on 64-bit specific division algorithm for a fixed-point math library.

I am currently writing a fast 32.32 fixed-point math library. I succeeded at making adding, subtraction and multiplication work correctly, but I am quite stuck at division. A little reminder for those who can't remember: a 32.32 fixed-point number is a number having 32 bits of integer part and 32 bits of fractional part. The best algor...

Flattening intersecting timespans

I have lots of data with start and stop times for a given ID and I need to flatten all intersecting and adjacent timespans into one combined timespan. The sample data posted below is all for the same ID so I didn't list it. To make things a bit clearer, take a look at the sample data for 03.06.2009: The following timespans are overlap...

Choose random array element satisfying certain property

Suppose I have a list, called elements, each of which does or does not satisfy some boolean property p. I want to choose one of the elements that satisfies p by random with uniform distribution. I do not know ahead of time how many items satisfy this property p. Will the following code do this?: pickRandElement(elements, p) rand...

Produce a sentence from a grammar with a given number of terminals

Say you've got a toy grammar, like: (updated so the output looks more natural) S -> ${NP} ${VP} | ${S} and ${S} | ${S}, after which ${S} NP -> the ${N} | the ${A} ${N} | the ${A} ${A} ${N} VP -> ${V} ${NP} N -> dog | fish | bird | wizard V -> kicks | meets | marries A -> red | striped | spotted e.g., "the dog kicks the red wizard...

Union of All Intersecting Sets

Given a list of objects with multiple attributes I need to find the list of sets created by a union of all intersecting subsets. Specifically these are Person objects, each with many attributes. I need to create a list of 'master' sets based on a handful of unique identifiers such as SSN, DLN, etc. For instance, if Person A and Person...

sorting differences

I am currently learning a number of different sorting algorithm. being curious with the differences,I tried to find the information of them but none is good enough. so here is my questions, in term of performance and their concept, what is the differences among bubble sort, selection sort, insertion sort, shell sort and quick sort. ...

Speed dating algorithm

I work in a consulting organization and am most of the time at customer locations. Because of that I rarely meet my colleagues. To get to know each other better we are going to arrange a dinner party. There will be many small tables so people can have a chat. In order to talk to as many different people as possible during the party, ever...

Techniques for sorting up/down voted items

I'm pulling items from a database (in this case, comments, but it should apply to anything) and have the following information about them: The number of up votes The number of down votes (consequently, the total score and number of votes) The date the item was created I'm interested in coming up with a way to sort them with the followin...