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.
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.