algorithm

Need code for addition of 2 numbers

I am having the numbers follows taken as strings My actual number is 1234567890123456789 from this i have to separate it as s=12 s1=6789 s3=3456789012345 remaining as i said I would like to add as follows 1+3, 2+4, 6+5, 7+6, 8+7, 9+8 such that the output should be as follows 4613579012345 Any help please ...

Straight selection sort vs. exchange selection sort

What is the difference between straight selection sort vs. exchange selection sort? I got into a little debate today - my professor uses these two terminologies in his lecture notes. The selection sort that Wikipedia and any textbook or website will give you is what he is calling "exchange selection sort". I've never heard the term "exc...

Level-order tree traversal

Hi. I am trying to write a method that will take an IntTree as a parameter and return a Queue with the values of the IntTree in level order. To clarify: an IntTree is a tree with an integer value as its root, and has an IntTree as its left and right subtrees. A note on some methods: value(t) - returns the integer value at the root of t...

Script to convert Huge Three column table into table

I have a set of data (csv files) in the following 3 column format: A, B, C 3277,4733,54.1 3278,4741,51.0 3278,4750,28.4 3278,4768,36.0 3278,4776,50.1 3278,4784,51.4 3279,4792,82.6 3279,4806,78.2 3279,4814,36.4 And I need to get a three-way contingency table like: (sorry, this doesn't look completely good) A /B 4733 ...

Shortening an array based on consecutive occurences.

I'm in the middle of learning PHP, and the following list-related problem has come up. The language doesn't really matter, so I'll give you this in pseudo-code. Pseudo-code answers are fine, of course. Say, there's a list of two different, repeating elements - two single characters, for instance. So my list looks roughly like this: myL...

How should I map long to int in hashCode()?

I have a range of objects that have a long field whose value uniquely identifies a particular object across my entire system, much like a GUID. I have overriden Object.equals() to use this id for comparison, beause I want it to work with copies of the object. Now I want to override Object.hashCode(), too, which basically means mapping my...

Storing pixels in a byte array

Hi Guys, This was asked in one of the interviews and i cant seem to find any clue about it. The question is: I have 40 x 20 screen, and i want to store these pixels in a byte array so that i can reconstruct this screen again from the byte array. The size of byte array is 100bytes. Another way of looking at it is. How do we store a si...

transform tuples to tree

I have a directed acyclic graph(tree) described by (Parent1 -> Child1), (Parent2 -> Child2), ...., (ParentN -> ChildN) tuples. Is there any algorithm that reconstructs the tree(graph) from these informations? A better example: Root Parent1 Node1 Child1 Child2 Parent2 Node1 Child1 Child2 and a...

Trouble implementing collision in a breakout clone.

For this program, I've been using the OpenGL-based reni2D library in Visual C++ 2008 Express Edition. The said library is on this webpage: http://www.involuntaryexercise.com/apps/reni2D.zip The problem I get happens when trying to make a brick from the sprite structure. I've been using an array of bricks, but at the moment have restrict...

Algorithm to calculate the number fortnightly occurring events in a given calendar month.

I'm looking for the cleverest algorithm for determining the number of fortnightly occurring events in a given calendar month, within a specific series. i.e. Given the series is 'Every 2nd Thursday from 7 October 2010' the "events" are falling on (7 Oct 2010, 21 Oct, 4 Nov, 18 Nov, 2 Dec, 16 Dec, 30 Dec, ...) So what I am after is a fun...

Question on binary search tree

Hi, I am looking at the following code: http://netrsc.blogspot.com/2010/04/net-c-binary-tree.html Am I right in thinking that the while (!Found) condition will iterate the tree? protected void Insert(T item) { TreeNode<T> TempNode = Root; bool Found=false; while (!Found) { int ComparedValue = TempNode.Value.Com...

Beating a greedy algo

For class we have a grid and a bunch of squares on the grid that we need to detect and travel to. We start at (0,0). We scan tiny regions of the grid at a time (for reasons regarding our data structure it's mandatory), and when we detect squares that we need to travel, and then we travel. There are 32 locations on the grid, but we only n...

SIMD optimization puzzle

I Want to optimize the following function using SIMD (SSE2 & such): int64_t fun(int64_t N, int size, int* p) { int64_t sum = 0; for(int i=1; i<size; i++) sum += (N/i)*p[i]; return sum; } This seems like an eminently vectorizable task, except that the needed instructions just aren't there ... We can assume that N i...

few simple questions about binary search algorithm

(1) consider the SelectionSorter method sort. Which of the following statements is true at the beginning of each iteration of the loop? explain a. for all j, such that 0 <= j < i, 0 <= a[j] <= a[i]. b. for all j, such that 0 <= j < i, a[j] is in its final position in the sorted array. c. for all j, such that 0 <= j < i, a[0...

passing mysql rows to scoring algorithm?

i have this php algorithm ranking function(hacker news), function calculate_score($votes, $item_hour_age, $gravity=1.8){ return ($votes - 1) / pow(($item_hour_age+2), $gravity); } and i have this table in mysql: post{id, post_text, votes, date}: i was wondering how i can pass these mysql data paramters to that function, to det...

Algorithm for piecing together a sequence from multiple fragments

I am working on a real-time embedded system. I am trying to create a detailed timing analysis. I have collected runtime data, recording the start and stop time of each interrupt. Each burst of data looks something like this ISR# time ----- ---- 1 34 end 44 4 74 3 80 end 93 end 97 ... My output c...

Modifying selection algorithm to select weighted items

Hi guys, I am attempting to modify the selection algorithm to work with the following situation: My imput is a list of numbers x1, x2, ... , xn (not necessarily ordered). Each of these numbers has a weight "w". I call W the sum of all the weights. If I provide a value X, which is greater than 0 but no larger than W, how can I find a ...

Programming problem - Game of Blocks

Hi guys, maybe you would have an idea on how to solve the following problem. John decided to buy his son Johnny some mathematical toys. One of his most favorite toy is blocks of different colors. John has decided to buy blocks of C different colors. For each color he will buy googol (10^100) blocks. All blocks of same color are of sa...

iOS image comparison algorithm to find the differences in two very similar but different photos

My use case is that I have two images (photos) that are almost identical but do have some differences throughout. They won't be perfectly aligned, but they will be pretty darn close to each other. As a rough estimate, I'd expect to find a half dozen or less differences scattered randomly throughout a pair of 640x480 photos. The size o...

Insertion sort debug help

The following C code doesn't work (it just clears the list): /* Takes linkedlist of strings */ static int insertSort (linkedlist *list) { linkedlist sorted; void *data; node *one, *two, *newnode; unsigned int comp, x; removeHeadLL (list, &data); initLL (&sorted); addHeadLL (&sorted, data); while (list->count) { rem...