View Full Version : VBScript find missing number function??
mat41
05-17-2006, 04:49 AM
Good day
Is there a VBScript function to find missing numbers. Eg if I had a string:
1,2,3,4,5,6,7,8,9,14,10,11,12,13,15,16,17,21
My objective would be to return the values 18,19,20
I am sure I have used it before however can not find it in my code library, any vbScript .chm files and other documentation I have.
TYIA
Spudhead
05-17-2006, 01:00 PM
function findMissingNumbers(s)
missingnumbers = ""
maxNum = 0
aNums = split(s,",")
for c=lBound(aNums) to uBound(aNums)
num = cInt(aNums(c))
if num > maxNum then maxNum = num
next
for c = 1 to maxNum
exists = false
for d = lBound(aNums) to uBound(aNums)
if cInt(aNums(d)) = c then exists = true
next
if not exists then missingnumbers = missingnumbers & c & ","
next
if missingnumbers <> "" then missingnumbers = left(missingnumbers, len(missingnumbers)-1)
findMissingNumbers = missingnumbers
end function
Can you tell I'm bored at work? :thumbsup:
mat41
05-18-2006, 01:44 AM
SpudHead
Thank you for your time, this is appreciated. I have run your function passing the following string:
1,2,3,4,5,6,7,8,9,14,10,11,12,13,15,16,17,18,21,23
It returned:
1,2,3,4,5,6,7,8,9
I would expect it to return:
19,20,22
FYI: The function showed an invaid use of cInt on the following two lines:
num = cInt(aNums(c))
and
if cInt(aNums(d)) = c then exists = true
This comer delimited string is not sequential. Additioanly is I pass:
findMissingNumbers("1,2,4,5,6")
I would expect it to return:
3
It returns:
1,2,3,4,5,6
My objective of this post is to show the missing numbers
TYIA
Spudhead
05-18-2006, 12:59 PM
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%
function findMissingNumbers(s)
missingnumbers = ""
maxNum = 0
aNums = split(s,",")
for c=lBound(aNums) to uBound(aNums)
num = cInt(aNums(c))
if num> maxNum then maxNum = num
next
for c = 1 to maxNum
exists = false
for d = lBound(aNums) to uBound(aNums)
if cInt(aNums(d)) = c then exists = true
next
if not exists then missingnumbers = missingnumbers & c & ","
next
if missingnumbers <> "" then missingnumbers = left(missingnumbers, len(missingnumbers)-1)
findMissingNumbers = missingnumbers
end function
response.write(findMissingNumbers("1,2,3,4,5,6,7,8,9,14,10,11,12,13,15,16,17,18,21,23") & "<br/>" & vbCrLf)
response.write(findMissingNumbers("1,2,4,5,6") & "<br/>" & vbCrLf)
%>
</body>
</html>
I don't know what you're doing, but it's not the same as what I'm doing :confused:
mat41
02-12-2007, 07:25 AM
Spudhead
The code you kindley assisted me with has been working just fine up until recentley. The working function are:
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
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,4 3,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 assistanc
mat41
02-12-2007, 09:52 AM
I have now discovered if i remove the last integer value (24) from the comer delimited string above it works as it should. mmmm, very strange. Is my function(s) a bit tired and therefore lack scalability? The problem doesnt seem to be because the string is not ordered sequentialy however if they are placed this way it works, mmmmm...
To see this, if one has the time, run the following code. The temporaraly hard coded 'finalString' value works with the 24 removed from the end. After you run the code comment out the 'finalString' value being used and un comment the one above, this is the problematic string:
NOTE: the output has /06 appended to the end. The numbers being run through the function are what preceeds the forward slash.
<%
Function findMissing(str, delim)
'' first split the string to get an array
'' 2nd sort the array
'' 3rd, look for the missing numbers in sequence, using mat41s method
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
'finalString = "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,4 3,44,45,24"
finalString = "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,4 3,44,45"
If len(finalString) > 2 Then
missingNumArray = split(findMissing(finalString, ","),",")
For numCount2 = 0 To ubound(missingNumArray)-1
response.write missingNumArray(numCount2) & "/0" & right(osirNum, 1) & ", "
Next
Set missingNumArray = nothing
End if
%>
Any ideas would be much appreciated TYIA
Spudhead
02-12-2007, 01:29 PM
<%
function findMissingNumbers(s, delim)
missingnumbers = ""
maxNum = 0
aNums = split(s,delim)
'find max number
for c=lBound(aNums) to uBound(aNums)
num = cInt(aNums(c))
if num > maxNum then maxNum = num
next
for c = 1 to maxNum
exists = false
for d = lBound(aNums) to uBound(aNums)
if cInt(aNums(d)) = c then exists = true
next
if not exists then missingnumbers = missingnumbers & c & ","
next
if missingnumbers <> "" then missingnumbers = left(missingnumbers, len(missingnumbers)-1)
findMissingNumbers = missingnumbers
end function
response.write(findMissingNumbers("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,4 3,44,45,24", ","))
%>
outputs: 14,15,23,34,41
mat41
02-13-2007, 01:22 AM
This one function is much more efficient than the two? Thanking you very much for your time.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.