algorithm

What is the best algorithm to calculate the most scored item?

I have an music items that are scored by users between 1 to 5, and I need a formula to get the 5 most scored items. But obviously an item that get 3.5 average score from 1000 different users will be more scored then an item thet get 4.9 average score from only 5 users... in other words I think that if an item get attention from people ...

Looking for fast algorithm to find distance between two nodes in binary tree

How do I find the distance between two nodes in a binary tree? Equivalently, what algorithms are there for finding the most recent common ancestor (lowest common ancestor) of two nodes? ...

How can I get the possessive form of a noun?

Here's an algorithm for adding an apostrophe to a given input noun. How would you contruct a string to show ownership? /** * apostrophizes the string properly * <pre> * curtis = curtis' * shaun = shaun's * </pre> * * @param input string to apostrophize * @return apostrophized string or empty string if the input was empty or nul...

What is the cleanest way to create a timeout for a while loop?

Windows API/C/C++ 1. .... 2. .... 3. .... 4. while (flag1 != flag2) 5. { 6. SleepEx(100,FALSE); //waiting for flags to be equal (flags are set from another thread). 7. } 8. ..... 9. ..... If the flags don't equal each other after 7 seconds, I would like to continue to line 8. Any help is appreciated. ...

efficient Python library to recommend product based on user history

I have a database of which products every user has viewed and I want to recommend a product based on what similar users have viewed. Is there a Python library that can achieve this? I don't need Netflix quality results, just products that are probably of interest. Any ideas? ...

Data structure for mapping URLs or local paths

Hi, I've been trying to workout a good way of doing this fast but I'm not sure what will be the most optimal, I'm hoping some of you more experienced developers can assist via your Data Structures knowledge :-) Essentially I have a list of paths (Eg. C:\inetpub\wwwroot\, C:\www\websites\vhosts\somesite.com\, D:\www-mirror\websites\vhos...

Simplest C# implementation of A* algorithm?

Hi all, I'm looking for a simple implementation of A* (A star) algorithm in C#. Any pointers? Thanks you. ...

diff'ing without grouping unrelated blocks

Is there a diff algorithm that does not group unrelated blocks? For example: hello world lorem ipsum dolor sit amet vs. Hello World Lorem Ipsum Dolor Sit Amet Comparing these (e.g. with standard Unix diff) generally results in the following: < hello world < lorem ipsum dolor sit amet --- > Hello World > Lorem Ipsum Dolor Sit Amet...

How to determine whether a variable is a decimal and it is less than 0.01 in PHP?

How to determine whether a variable is a decimal and it is less than 0.01 in PHP? If I write if($balance<0.01) Will a true value be returned if $balance is not a decimal at all? ...

Guessing the hash function?

I'd like to know which algorithm is employed. I strongly assume it's something simple and hopefully common. There's no lag in generating the results, for instance. Input: any string Output: 5 hex characters (0-F) I have access to as many keys and results as I wish, but I don't know how exactly I could harness this to attack the functio...

Detecting self crossing in closed Bezier curves

I've created a "blob" shape by patching cubic Bezier curves together (screenshot below). I'd like to be able to detect the situation where a curve has crossed over either itself or another curve and was wondering if there's a recommended approach or known algorithm for doing this? One idea I had was to use a FlatteningPathIterator to d...

What is the BCL equivalent of GetValueElseAdd in PowerCollections

In Wintellect's PowerCollections, there's a GetValueElseAdd which works like this: if ( collection.GetValueElseAdd( key, ref value)) { // just added, value unmodified } Or // factory method only gets called if the key is not present. if ( collection.GetValueElseAdd( key, out value, () => thingFactory.CreateNew())) { // just a...

FA: Choosing Rotation matrix, based on "Simple Structure Criteria"

One of the most important issues in using factor analysis is its interpretation. Factor analysis often uses factor rotation to enhance its interpretation. After a satisfactory rotation, the rotated factor loading matrix L' will have the same ability to represent the correlation matrix and it can be used as the factor loading matrix, inst...

Select random k elements from a list whose elements have weights

Selecting without any weights (equal probabilities) is beautifully described here. I was wondering if there is a way to convert this approach to a weighted one. I am also interested in other approaches as well. Update: Sampling without replacement ...

Algorithm for creating cells by spiral on the hexagonal field

Help to find an algorithm for creating cells by spiral on the hexagonal field. Look at the image: Let's imagine an dimensionless 2d array. The X axis is the blue line, Y is horizontal, spiral is red. I need to add cells from the central point x0y0 to point N by spiral Tell me the way to solve the problem, please. Thank you! ...

Moving a unit precisely along a path in x,y coordinates

I am playing around with a strategy game where squads move around a map. Each turn a certain amount of movement is allocated to a squad and if the squad has a destination the points are applied each turn until the destination is reached. Actual distance is used so if a squad moves one position in the x or y direction it uses one point, b...

Grid based puzzle board game block removal algorithm

I have a "samegame" grid represented by a 1D array of integers. 0 to 63 to represent an 8x8 grid. the rules are that same coloured blocks of two or more can be removed by clicking on them. blocks then slide down from above. if a column is empty columns other columns move in from the sides. when someone clicks on the green blocks in t...

4 program design interview questions

I was preparing for technical interviews and would like to know how could I go about briefly explaining an interviewer about the approach to designing the following programs without going into unnecessary details 1. Program that lets people play tic tac toe with each other over the internet? 2. A suitable data structure f...

Right Shift to Perform Divide by 2 On -1

I know that I can perform divide by 2 using right shift. For simplicity, take a 4 bit number system -1 - 1111 -2 - 1110 -3 - 1101 -4 - 1100 -5 - 1011 -6 - 1010 -7 - 1001 -8 - 1000 7 - 0111 6 - 0110 5 - 0101 4 - 0100 3 - 0011 2 - 0010 1 - 0001 0 - 0000 If I try to perform 6 / 2 = 0110 >> 1 = 0011 = 3 -6/ 2 = 1010 >> 1 = 1101...

What's the best way to position interconnected nodes in a 2d graph?

I have a list (graph?) of nodes, and each node is connected to other nodes in the list one or more times. I want to take these nodes and lay them out in a nice 2d diagram with lines connecting them together in an optimal way. What's the best algorithm for doing this so that they're spaced evenly apart and the lines connecting them are ...