algorithm

Recurrence Relation

Why is the recurrence relation of recursive factorial algorithm this? T(n)=1 for n=0 T(n)=1+T(n-1) for n>0 Why is it not this? T(n)=1 for n=0 T(n)=n*T(n-1) for n>0 Putting values of n i.e 1,2,3,4...... the second recurrence relation holds(The factorials are correctly calculated) not the first one. Please clear my doubt. ...

Splitting up a list into parts of balanced lengths

Hi I need an algorithm which given a list L and a number N, returns a list of N smaller lists where the sublists are "balanced". Examples: algo(range(1, 8), 3) -> [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -> [[1,2], [3], [4], [5]] algo(range(1, 12), 5) -> [[1,2,3], [4,5], [6,7], [8,9], [10, 11]] As you can see, the algorithm sh...

Checking string has balanced parentheses

I am reading the Algorithm Design Manual Second Edition and this is from an exercise question. Quoting the question A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string ((())())() contains properly nested pairs of paren...

How to calculate the number of days between two given dates? (Leap year obstacle)

Any year evenly divisible by 400 is a leap year (e.g., 2000 was a leap year). Any other year evenly divisible by 100 is not a leap year (e.g., 1700, 1800 and 1900 were not leap years). Any other year evenly divisible by 4 is a leap year (e.g., 1996 and 2004 are leap years). But I'm not sure how to make nested if states in my c-program...

Calculate the number of days a person has been alive on his Nth birthday? (Leap year challenge)

How to calculate the number of days that person has been alive on his Nth birthday? given his birth month, day, year, and his age? How to solve the leap year challenge? ...

Drawing irregular concentric circles using Google Maps

I have a bit of a problem. I am trying to do the following using Javascript & the Google Maps API v2: I can draw individual circles just fine using formulas found all over the Internet. The problem I am facing is that the circles must: A. Be concentric, and B. Must have different radius for each "quadrant", i.e., NE, NW, SE & SW I'...

URL Shortening Site

I am working on a URL shortening site which uses PHP, MySQL and Apache. General idea of URL shortening as I look at open source projects: user gives a URL link and the system gets ID for that link from database. Then convert the ID X base system (I am using base 36). Then use Apache mod_rewrite and create shortened URL and then redirect....

Grouping and sorting algorithm help

Lets say I have a million people objects that, when evaluated using person1.Matches(person2); return true or false. I want to put them into groups. These groups are made by any one person being a match with any other person in the group. So any person from one group will NOT be a match with any person from another group. Any person in...

Recursive text conversion

Given the following: > This is level 1 > This is level 2 >> This is level 2.1 >> This is level 2.2 >>> This is level 2.2.1 >>> This is level 2.2.2 > This is level 3 How would you convert that text to XHTML, without a parser library such as ANTLR? That is: <ul> <li>This is level 1</li> <li>This is level 2 <ul>...

ArrayList without the copying overhead?

Does anyone know of a List implementation that has a constant time get(int index) (I.e. implements RandomAccess) but doesn't have to copy the whole list when it grows as ArrayList does? I'm thinking the implementation may well be in terms of other lists e.g. public class ChunkedList<T> implements List<T>, RandomAccess { private Linke...

I need some tips on how to prepare for the ICPC Programming Competition!

I'm participating in the Competencia Latinoamericana de Programacion this month and I was wondering if any of you have ever programmed competitively. The contest is basically 10 questions and I have to design an algorithm for each question with a team of 2 other people. What are some things I should be ready for? What exercises should ...

Calculating factorial of large numbers in C

Hello, In my C code , i want to calculate the factorial for numbers in the range 1 to 100. For small numbers, the function works but for bigger numbers for example 100! it returns incorrect result. Any ways to handle factorial of large numbers in C ?. The compiler im using is gcc v4.3.3 . My code is as follows : #include <stdio.h> #inc...

Weighted Item Algorithm

Hi everyone, I would like to learn how to select weighted items. For example : I want to fetch questions from a pool but if some one can't give right answer to a question, that causes this question to double its weight and increase the probability of being selected again later on. ...

Project Euler N2 - Fibonacci algorithm isnt working properly.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. Int64[] Numeros = new Int64[400000...

Damerau-Levenshtein distance for words

I am looking for such an algorithm, but one that makes substitutions between words and not between letters. Is there such an algorithm? I am looking for an implementation with SQL Server, but the name of the algorithm will be good enough. ...

Help me implement a rewindable buffer

This is where I'm so far: I have the following the following use case (as impartial jUnit test) to show what I want to achieve: buffer.add(o1); assertEquals(1, buffer.size()); buffer.mark(); buffer.add(o2); assertEquals(2, buffer.size()); buffer.add(o3); assertEquals(3, buffer.size()); assertSame(o1, buffer.get()); assertSame(o2, buffe...

Handling big numbers in code

Hello, Im working on a programming problem where i need to handle a number involving 100000 digits . Can python handle numbers this big ? Thank You ...

Match text in loops in Ruby

I have to go through the following text and match each of the following, and break them apart into separate records to save to a database. So this text: ESTIMATED MINIMUM CENTRAL PRESSURE 951 MB EYE DIAMETER 12 NM MAX SUSTAINED WINDS 105 KT WITH GUSTS TO 130 KT 64 KT....... 25NE 25SE 25SW 25NW 50 KT....... 60NE 30SE 30SW 60NW 34...

towers of hanoi variation pseudocode

This is a variation to the original towers of hanoi problem. The same rules apply but instead of just one stack of n disks there are two. One stack of red disks on the left pole and another stack of purple disks on the right. The final configuration should be the purple on the left and red on the right. There are a total of 3 poles. I'm ...

Find median value from a growing set.

I came across an interesting algorithm question in an interview. I gave my answer but not sure whether there is any better idea. So I welcome everyone to write something about his/her ideas. You have an empty set. Now elements are put into the set one by one. We assume all the elements are integers and they are distinct (according to th...