algorithm

Reverse the ordering of words in a string

Hi, I have this string s1 = "My name is X Y Z" and I want to reverse the order of the words so that s1 = "Z Y X is name My". I can do it using an additional array. I thought hard but is it possible to do it inplace (without using additional data structures) and with the time complexity being O(n)? cheers ...

Nested grouping strategy/algorithm c#

Not sure if I have to correct label for this type of problem, but do you have any thoughts on a generic solution for the following? Given a collection of Invoices: var invoices = new List<Invoice>() { new Invoice() { Id = 1, Customer = "a", Date = DateTime.Parse("1/1/2009") }, new Invoice() { Id = 2, Customer = "a", Date = DateTime.Par...

How to distribute proportionally dates on a scale with Python

I have a very simple charting component which takes integer on the x/y axis. My problem is that I need to represent date/float on this chart. So I though I could distribute proportionally dates on a scale. In other words, let's say I have the following date : 01/01/2008, 02/01/2008 and 31/12/2008. The algorithm would return 0, 16.667, an...

Python factorization

I'd just like to know the best way of listing all integer factors of a number, given a dictionary of its prime factors and their exponents. For example if we have {2:3, 3:2, 5:1} (2^3 * 3^2 * 5 = 360) Then I could write: for i in range(4): for j in range(3): for k in range(1): print 2**i * 3**j * 5**k But here I've got 3 ...

Is possible to make the quick sort fuction sort array descending?

I got the following php function, and want to change it into descending sorting, can someone help me: function quickSort(&$numbers, $array_size,$level) { q_sort($numbers, 0, $array_size - 1,$level); } function q_sort(&$numbers, $left, $right,$level) { $l_hold = $left; $r_hold = $right; $pivot = $numbers[$left]; while...

Java equivalent of C#'s Rfc2898DerivedBytes

Hi. I was wondering if anyone have tried to do an equivalent of Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(secret, saltValueBytes); byte[] secretKey = key.GetBytes(16); in Java. Where secret is a string(password), and saltValueBytes is, well, a salt in byte array. I've tried stuff, but can't seem to wrap my head around it. Tha...

A* heuristic, overestimation/underestimation?

I am confused about the terms overestimation/underestimation. I perfectly get how A* algorithm works, but i am unsure of the effects of having a heuristic that overestimate or underestimate. Is overestimation when you take the square of the direct birdview-line? And why would it make the algorithm incorrect? The same heuristic is used f...

Multicore - how to merge local groups of data found on each core?

I have a large set of scalar values distributed over a 3D mesh (one value per vertex.) My goal is to show: all points in the mesh where the value is greater than a threshold. AND group the points that are connected (to simplify the display.) So my basic solution was: Find the points that pass the threshold test For each point th...

Secure algorithm for creating license keys?

I would like to distribute an application, but have license key that they can enter to unlock. What is a good algorithm to create a concise key that contains information about what version they have purchased, as well as additional things such as duration of license, etc. I realize this protection can be cracked, but it keeps honest pe...

How to write a search engine?

I am trying to write a search engine that will give meaningful results when a user enters words that occur within a bunch of documents. For that I want to know how exactly a search engine works, what data structures and algorithms it uses to build indexes, store and query indexes etc. Plus some pointers on how to make a search engine giv...

Find whether a tree is a subtree of other

There are two binary trees T1 and T2 which store character data, duplicates allowed. How can I find whether T2 is a subtree of T1 ? . T1 has millions of nodes and T2 has hundreds of nodes. ...

How many numbers below N are coprimes to N?

In short: Given that a is coprime to b if GCD(a,b) = 1 (where GCD stands for great common divisor), how many positive integers below N are coprime to N? Is there a clever way? Not necessary stuff Here is the dumbest way: def count_coprime(N): counter = 0 for n in xrange(1,N): if gcd(n,N) == 1: counter +...

What type of Algorithm would you use to help forecast future performance based on historical data?

In the past we have been using a summary of historical performance by "Vendor" to decide how we allocate new business to each vendor. Now what we want to do is to break that summary performance into smaller subsets such geography, size, age, etc to better allocate the new business we hand out to our vendors. For example, Lets say that ...

Is there a better way to escape this string?

What is the quickest way in ColdFusion (or Java) for converting a string as such: Input: 79827349837493827498 Output: \79\82\73\49\83\74\93\82\74\98 I'm taking an ldap guid and escaping it for a query. I can do it as a series of MID reductions like this: <CFSET V1 = ""> <CFSET RetVal = ""> <CFLOOP CONDITION="#V1# NEQ''"> ...

What's a good data model for cross-tabulation?

I'm implementing a cross-tabulation library in Python as a programming exercise for my new job, and I've got an implementation of the requirements that works but is inelegant and redundant. I'd like a better model for it, something that allows a nice, clean movement of data between the base model, stored as tabular data in flat files, a...

Finding Start and End Dates from Date Numbers Table (Date Durations)

I have two tables: a schedule table that contains information about how an employee is scheduled and a numbers table in which each number corresponds to a date. The tables look like: [Employee Schedule] ID Employee ID Project ID Day ID ----------- ----------- ----------- ----------- 1 64 2 168 2 ...

Speed of calculating powers (in python)

I'm curious as to why it's so much faster to multiply than to take powers in python (though from what I've read this may well be true in many other languages too). For example it's much faster to do x*x than x**2 I suppose the ** operator is more general and can also deal with fractional powers. But if that's why it's so much slowe...

Time-series and correlation strategies

I have various time-series I'd like to correlate and present as either a csv-file or in-memory datatable (.NET). These time-series are arrays of time-value-pairs (actually these are objects containing more than just time and value). The time-series may span across different overlapping periods, and some might even have holes (missing val...

Fast average without division

I have a binary search loop which gets hit many times in the execution path. A profiler shows that the division part of the search (finding the middle index given the high and low indices of the search range) is actually the most costly part of the search, by a factor of about 4. (I think) it is not critical for efficient binary search...

C# Array Subset fetching

I have an array of bytes and i want to determine if the contents of this array of bytes exists within another larger array as a continuous sequence. What is the simplest way to go about doing this? ...