Function findMissing(str, delim)
findMissing = ""
ar = Split(str, delim)
ar = sortIt(ar)
For i = 0 To UBound(ar) - 1
If CLng(ar(i)) + 1 <> CLng(ar(i + 1)) Then
'' we found missing number(s)
For j = 1 To (CLng(ar(i + 1)) - CLng(ar(i))) - 1
findMissing = findMissing & CStr(CLng(ar(i)) + j) & ","
Next
End If
Next
End Function
Function sortIt(ar)
For j = 0 To UBound(ar)
For i = 0 To UBound(ar) - 1
If CLng(ar(i)) > CLng(ar(i + 1)) Then
tmp = ar(i)
ar(i) = ar(i + 1)
ar(i + 1) = tmp
Exit For
End If
Next
Next
sortIt = ar
End Function
The function(s) above have been working well up until the end of last year. This week its come to my attention that:
> If the string is reasonably sequential it does what its supposed to do - find the missing numbers.
> If not it fails in a way which I am having trouble understanding. Below is an example of a string which fails:
1,2,3,4,5,6,7,8,10,11,12,13,16,17,18,19,20,21,22,25,26,27,28,29,31,32,33,35,37,38,36,30,9,39,40,42,43,44,45,24
The missing numbers in this string are 14,15,23,34 and 41. If I manually order the string sequentialy it works fine however if passed to the function in the order above the result of missing numbers is:
14,15,23,24,34,25,26,27,28,29,30,31,32,33,34,35,36,37,41
I have been looking at it for to long today. I can not work out why it says 14 is the first missing number when sequentialy 9 is. Would anybody have the time to look over it and advise me on the following:
> Are my functions a bit rough?
> If not can anyone see a mistake I have made or point out the logic why it fails in the manor it does?
> Would creating a function that orders the string sequentialy before I pass it into the above functions be prudent?
This one has be backed in a corner TYIA for any assistance
findMissing = ""
ar = Split(str, delim)
ar = sortIt(ar)
For i = 0 To UBound(ar) - 1
If CLng(ar(i)) + 1 <> CLng(ar(i + 1)) Then
'' we found missing number(s)
For j = 1 To (CLng(ar(i + 1)) - CLng(ar(i))) - 1
findMissing = findMissing & CStr(CLng(ar(i)) + j) & ","
Next
End If
Next
End Function
Function sortIt(ar)
For j = 0 To UBound(ar)
For i = 0 To UBound(ar) - 1
If CLng(ar(i)) > CLng(ar(i + 1)) Then
tmp = ar(i)
ar(i) = ar(i + 1)
ar(i + 1) = tmp
Exit For
End If
Next
Next
sortIt = ar
End Function
The function(s) above have been working well up until the end of last year. This week its come to my attention that:
> If the string is reasonably sequential it does what its supposed to do - find the missing numbers.
> If not it fails in a way which I am having trouble understanding. Below is an example of a string which fails:
1,2,3,4,5,6,7,8,10,11,12,13,16,17,18,19,20,21,22,25,26,27,28,29,31,32,33,35,37,38,36,30,9,39,40,42,43,44,45,24
The missing numbers in this string are 14,15,23,34 and 41. If I manually order the string sequentialy it works fine however if passed to the function in the order above the result of missing numbers is:
14,15,23,24,34,25,26,27,28,29,30,31,32,33,34,35,36,37,41
I have been looking at it for to long today. I can not work out why it says 14 is the first missing number when sequentialy 9 is. Would anybody have the time to look over it and advise me on the following:
> Are my functions a bit rough?
> If not can anyone see a mistake I have made or point out the logic why it fails in the manor it does?
> Would creating a function that orders the string sequentialy before I pass it into the above functions be prudent?
This one has be backed in a corner TYIA for any assistance