algorithm

How to test a hash function?

Is there a way to test the quality of a hash function? I want to have a good spread when used in the hash table, and it would be great if this is verifyable in a unit test. EDIT: For clarification, my problem was that I have used long values in Java in such a way that the first 32 bit encoded an ID and the second 32 bit encoded another ...

Turn while loop into math equation?

I have two simple while loops in my program that I feel ought to be math equations, but I'm struggling to convert them: float a = someValue; int b = someOtherValue; int c = 0; while (a <= -b / 2) { c--; a += b; } while (a >= b / 2) { c++; a -= b; } This code works as-is, but I feel it could be simplified into math equ...

Counting the number of occurrences of words in a textfile

How could I go about keeping track of the number of times a word appears in a textfile? I would like to do this for every word. For example, if the input is something like: "the man said hi to the boy." Each of "man said hi to boy" would have an occurrence of 1. "the" would have an occurence of 2. I was thinking of keeping a diction...

Stackoverflow's Related Questions

Just wondering how you would go about implementing something similar to stackoverflow'd related questions. Would you simply match the tags, match similar words in the titles, or words in the entire question? Particular interested in a linqtosql method. Cheers! ...

Neatest / Fastest Algorithm for Smallest Positive Number

Hi, Simple question - In c++, what's the neatest way of getting which of two numbers (u0 and u1) is the smallest positive number? (that's still efficient) Every way I try it involves big if statements or complicated conditional statements. Thanks, Dan Here's a simple example: bool lowestPositive(int a, int b, int& result) { //ch...

How best to sum up lots of floating point numbers?

Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this: [1.0, 1e-10, 1e-10, ... 1e-10.0] and you add up from left to right with a simple loop, like sum = 0 numbers.each do |val| sum += val...

How do ASCII art image conversion algorithms work?

There are some nice free "image to ASCII art" conversion sites like this one: ASCII-art.org How does such an image conversion algorithm work? , . W , W W @ W ,W W ...

Representing continuous probability distributions

I have a problem involving a collection of continuous probability distribution functions, most of which are determined empirically (e.g. departure times, transit times). What I need is some way of taking two of these PDFs and doing arithmetic on them. E.g. if I have two values x taken from PDF X, and y taken from PDF Y, I need to get t...

Word Anagram Hashing Algorithm?

Given set of words, we need to find the anagram words and display each category alone using the best algorithm input: man car kile arc none like output: man car arc kile like none the best solution I am developing now is based on a hashtable, but I am thinking about equation to convert anagram word into integer value exmaple: man...

Checking if two strings are permutations of each other in Python

I'm checking if two strings a and b are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways: sorted(a) == sorted(b) and all(a.count(char) == b.count(char) for ch...

How can I create cells or grids in C++ for a randomized maze?

I'm trying to create a randomized maze in C++, but I can't start because I don't know how to create grids or cells. How could I create it? And I also want to create it using ASCII characters. how can i store it in array? (can any one give a sample code and some explanation so i can understand it better) Another question: What data stuct...

A better/good way to generate 4x4x4 Sudoku board?

For fun when I have time, I like to code games to see how hard it is or just because I want to see how it works from the inside or just for personal challenge. I just like coding. For now, I made these ones. So I made a Sudoku board. First it was the normal 3x3x3 board but then someone asked me to do a 4x4x4 board. I was successful but ...

Looping in a spiral

A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fellow SO'ers could come up with a better solution. I'm posting my solution as an answer to this question. Example Output: For a 3x3 matrix, the output should be: (...

finding long repeated substrings in a massive string

I naively imagined that I could build a suffix trie where I keep a visit-count for each node, and then the deepest nodes with counts greater than one are the result set I'm looking for. I have a really really long string (hundreds of megabytes). I have about 1 GB of RAM. This is why building a suffix trie with counting data is too ine...

Collect Lowest Numbers Algorithm

I'm looking for an algorithm (or PHP code, I suppose) to end up with the 10 lowest numbers from a group of numbers. I was thinking of making a ten item array, checking to see if the current number is lower than one of the numbers in the array, and if so, finding the highest number in the array and replacing it with the current number. ...

Finding characters in a string that occur only once

I'm writing an algorithm in PHP to solve a given Sudoku puzzle. I've set up a somewhat object-oriented implementation with two classes: a Square class for each individual tile on the 9x9 board, and a Sudoku class, which has a matrix of Squares to represent the board. The implementation of the algorithm I'm using is a sort of triple-tie...

Calculating the semantic distance between words

Does anyone know of a good way to calculate the "semantic distance" between two words? Immediately an algorithm that counts the steps between words in a thesaurus springs to mind. ...

Check linkedlist circularity

I need a method that takes a linkedlist as a parameter and return true or false if it is circular or not. ex:circular linkedlist means there is node pointes to any previous node. I forgot to tell some constraints, I can not use any datastructure or dynmaic memory allocations. I can use local variables only and the alogrithm can be done...

Connected points with-in a grid

Given a collection of random points within a grid, how do you check efficiently that they are all lie within a fixed range of other points. ie: Pick any one random point you can then navigate to any other point in the grid. To clarify further: If you have a 1000 x 1000 grid and randomly placed 100 points in it how can you prove that any...

Seeking an algorithm to efficiently layout calendar event banners

I'm looking for an algorithm to efficiently place all-day/multi-day event banners, much like the month view in Outlook or Google Calendar. I have a number of events with a begin and end date, ordered by increasing begin (and then end) date (or any other order you please, I'm gathering events from a database table). I would like to minimi...