algorithm

Need help with STL sort algorithm

I'm having some troubles with using the std::sort algorithm here. I was reading that you can just overload the less than operator to sort classes, but I have been getting all sorts of errors. I have also tried using a functor as you can see in the example I made below. I was hoping somebody could see what I'm doing wrong here. #include...

How would you print out the data in a binary tree, level by level, starting at the top?

This is an interview question I think of a solution. It uses queue. public Void BFS() { Queue q = new Queue(); q.Enqueue(root); Console.WriteLine(root.Value); while (q.count > 0) { Node n = q.DeQueue(); if (n.left !=null) { Console.Writeln(n.left); q.En...

PHP Weight Algorithm

I'm wondering if this is a sufficient algorithm for finding the best value with a weighted system. Is there anything I could add to make it better? Edit: in this example I would like the probability of $object->get() returning test4 to be 4 times greater than the probability of it returning test1 (thanks acrosman) Thanks class weight...

Algorithm challenge: Generate color scheme from an image

Background So, I'm working on a fresh iteration of a web app. And, we've found that our users are obsessed with being lazy. Really lazy. In fact, the more work we do for them, the more they love the service. A portion of the existing app requires the user to select a color scheme to use. However, we have an image (a screenshot of t...

Resize Image to fit in bounding box

An easy problem, but for some reason I just can't figure this out today. I need to resize an image to the maximum possible size that will fit in a bounding box while maintaining the aspect ratio. Basicly I'm looking for the code to fill in this function: void CalcNewDimensions(ref int w, ref int h, int MaxWidth, int MaxHeight); Wher...

Find all combinations of coins when given some dollar value

I found a piece of code that I was writing for interview prep few months ago. According to the comment I had, it was trying to solve this problem: Given some dollar value in cents (e.g. 200 = 2 dollars, 1000 = 10 dollars), find all the combinations of coins that make up the dollar value. There are only penny, nickel, dime, and quarter....

Finding the coordinates of tiles that are covered by a rectangle with x,y,w,h pixel coordinates

Say I have a tile based system using 16x16 pixels. How would you find out what tiles are covered by a rectangle defined by floating point pixel units? for eg, rect(x=16.0,y=16.0, w=1.0, h=1.0) -> tile(x=1, y=1, w=1, h=1) rect(x=16.0,y=16.0, w=16.0, h=16.0) -> tile(x=1, y=1, w=1, h=1) (still within same tile) rect(x=24.0,y=24.0, w=8....

An algorithm for inflating/deflating (offsetting, buffering) polygons

UPDATE: the math term for what I'm looking for is actually inward/outward polygon offseting. +1 to balint for pointing this out. The alternative naming is polygon buffering. Before I start developing my own solution from scratch, does anyone know of any good source for an algorithm that can inflate a polygon, something similar to this: ...

What is the best way to count words in C?

Here is a simple count word program, which i feel is very efficient. Is this the best way to count words in C, or are there any flaws in this program? #include <stdio.h> int CountWords(void); main() { printf("count the words and enter string\n"); CountWords(); } ...

Chess Optimizations

ok, so i have been working on my chess program for a while and i am beginning to hit a wall. i have done all of the standard optimizations (negascout, iterative deepening, killer moves, history heuristic, quiescent search, pawn position evaluation, some search extensions) and i'm all out of ideas! i am looking to make it multi-threaded ...

Simple orthogonal graph edge routing algorithm

Many graphing applications use orthogonal edge routing for "objects" on a canvas. I am aware of the brilliant collection of algorithms found in the open source project titled QuickGraph (http://quickgraph.codeplex.com/) but the layout is left to actual layout renderers (such as GraphViz (http://www.graphviz.org) and MSAGL (found on Mic...

How to find the smallest interval containing a set of values modulo N?

First the practical application that led me to the problem: Given a set of angle measurements v[i] in the range of [0,360) degrees, what is the smallest interval containing all v[i]? Note: the interval may be on both sides, close around 0. Abstract description of the problem: For a given set of values v[i], what are the values c and ...

Formula for calculating Exotic wagers such as Trifecta and Superfecta

I am trying to create an application that will calculate the cost of exotic parimutuel wager costs. I have found several for certain types of bets but never one that solves all the scenarios for a single bet type. If I could find an algorithm that could calculate all the possible combinations I could use that formula to solve my other pr...

searching a binary tree

I'm writing an iterative function to search a binary tree for a certain value. This is localized to signed ints until I get into how to genericize classes. Assume that my class is BinarySearchTree, and it has a pointer to the root node of the tree. Also assume that nodes are inserted through an insert function, and have pointers to two...

How can I match two songs?

What is the best way to match two songs? IE. Have a song stored in the database in whatever format is best and then play another song and match the two together? In Python ideally please. Thanks! ...

Interesting strings algorithm

Given two finite sequences of string, A and B, of length n each, for example: A1: "kk", A2: "ka", A3: "kkk", A4: "a" B1: "ka", B2: "kakk", B3: "ak", B4: "k" Give a finite sequences of indexes so that their concentration for A and B gives the same string. Repetitions allowed. In this example I can't find the solution but for example if...

Write a function that returns the longest palindrome in a given string

e.g "ccddcc" in the string "abaccddccefe" I thought of a solution but it runs in O(n^2) time Algo 1: Steps: Its a brute force method Have 2 for loops for i = 1 to i less than array.length -1 for j=i+1 to j less than array.length This way you can get substring of every possible combination from the array Have a palindrome fu...

Is there a simple "point in rect" algorithm for a wraparound map?

I'm trying to build a rectangular grid that can wrap around at the edges. Anyone who plays video games will probably be familiar with the concept: go far enough in one direction on the world map and you'll end up back where you started. This causes some difficulty in setting up the viewport, though, since the edges can scroll into nega...

Cost of len() function

What is the cost of len() function for Python built-ins? Is it same for all built-ins? (list/tuple/string/dictionary) ...

Collision detection of triangles in 3D with some movement

I'm looking for a pretty easy algorithm for a collision detection with two three-dimensional triangles, which can move constantly (rather better if the could accelerate, too). I've found a method to solve this problem but that's a difficult one with movement of the two three-dimensional triangles. ...