Click to See Complete Forum and Search --> : extracting sub lists from a list


ndgani
March 9th, 2009, 07:59 AM
Hello to all.
i'm a real newb to this site (first post), so go easy on me please.

my problem is as follows:

given a list L of integers, i need to recursively:
while L is not an empty list
find the minimal difference d between elements in L.
remove Cd,the list of all sublists with difference d, from L.
split all subLists in Cd by length k to sublists Cd,k.

example:
L = {2,3,5,7,8,12,14,17,18,20,21,22,24,25,27}
we can see that the minimal difference is 1 and we have several subLists of different lengths
with that difference:
{2,3},{7,8},{17,18},{20,21,22},{24,25}
now removing all those sublists from L will leave it looking like this
L = {5,12,14,27}
an we start all over again

finding the minimal difference is not a problem -

public int findMinDiff(List<int> L)
{
int minDiff = L[1] - L[0];
int curr = 0;
for (int i = 0; i < L.Count - 1; i++)
{
curr = L[i + 1] - L[i];
if (curr < minDiff)
minDiff = curr;
}
return minDiff;
}

if anyone can think of a better implementation i'm all ears.

my real problem is the other two lines of psuedo-code
dividing the list to sublists and removing all the elements from the original list.

i get tangled up in while loops and recursion,ending up with an infinite loop,
so i wont post the useless code i've come up with so far.

i'm looking for an efficient way (run thru L the least number of times) to get
Cd,k and empty L.

my implementation is in c#, but that not realy relevant.

help anyone?

p.s.
i read some threads before posting my own, and a repeating question is if this is homework.
well, to save us a post and a postback, this is part of my senior year project, so you could consider it homework, but not really.

thanks.

ProgramThis
March 10th, 2009, 12:46 PM
To make that recursive you need to find a few things first.

a.) What is your base case?

if(L.Count <=0)
return min;

Of course your function has to contain the min in the call:
function findMinDiff(List<int> L, int min)

b.) A way to check the list. How are you going to know where to start and where you are in the list? Well, you could always pass the argument in the method:
function findMinDiff(List<int> L, int min, int position)
...
int temp = Math.abs(L[position] -L[position + 1]);
if(temp > min)
min = temp;

c.) Recursive call.

return findMinDiff(L,min,position++);

This isn't exact code mind you, just the steps needed to make it recursive (if that is the requirement for the class).

If I have misunderstood you and the requirement was to make removing them from the list recursive then that is another story, but similar procedure. Removing them from the sublist is a call to another subroutine. That cannot be done in this method because you haven't found the actual min diff yet. So, once this method returns the min diff you call another function to remove all pairs whose difference is = to the value returned from the function.

You keep calling those two methods until the list <= 1 (or whatever your teacher has instructed, empty list or last element standing, which of course depends on odd or even number of elements).

So recursively:

function foo(List<int> L)
if(L.Count <=1)
return;
return foo(someOtherFunction(findMinDiff(L,0,1)));

Or something like that. Implementation is up to you.