step Posted August 3, 2005 Posted August 3, 2005 i have an array of numbers (always 9) could be .... 0,0,5,1,0,9,2,0,0 so... basically i want to find all the missing numbers! in this case the missing numbers are.... 3,4,6,7 and 8 any cool ideas
seandisanti Posted August 3, 2005 Posted August 3, 2005 i have an array of numbers (always 9)could be ....0,0,5,1,0,9,2,0,0so... basically i want to find all the missing numbers!in this case the missing numbers are....3,4,6,7 and 8any cool ideas<{POST_SNAPBACK}>if the numbers will always be single digit, you don't even need an array, you could just use a for loops with StringInStr to check each number if it's there. but here's a pretty simple way...#include <array.au3> dim $Numbers[9] $numbers = StringSplit(InputBox("paste in number string","paste in number string"),",") $notin = "" for $i = 0 to 9; check if number exists $in = 0 for $i2 = 0 to 8; check each element if $numbers[$i2] = $i then $in = 1 Next if $in = 0 then if StringLen($notin) < 1 then $notin = $i Else $notin = $notin & "," & $i EndIf EndIf Next MsgBox(0,"Not in","These numbers were not in the array" & @crlf & $notin)
herewasplato Posted August 3, 2005 Posted August 3, 2005 @cameronsdad, Just for the fun of it (and to learn a bit) I compiled the code that you offered and then decompiled it. I wondered if array.au3 would be included in the compiled version even though it is not used in the rest of the script - it was. Never mind me - just answering questions that no one is asking...atm [size="1"][font="Arial"].[u].[/u][/font][/size]
blindwig Posted August 3, 2005 Posted August 3, 2005 i have an array of numbers (always 9)could be ....0,0,5,1,0,9,2,0,0so... basically i want to find all the missing numbers!in this case the missing numbers are....3,4,6,7 and 8any cool ideas<{POST_SNAPBACK}>here's an idea:Func FindMissing($aIn, $iBase) If $iBase Then $iBase = 1 Local $Max =$aIn[0] Else $iBase = 0 $Max = UBound($aIn) - 1 EndIf _ArraySort($aIn, 0, 1) Local $aOut[$aIn[$Max]], $i, $j For $i = $iBase to $Max - 1 If $aIn[$i] + 1 <> $aIn[$i + 1] Then For $j = $aIn[$i] + 1 to $aIn[$i + 1] - 1 $aOut[0] = $aOut[0] + 1 $aOut[$aOut[0]] = $j Next EndIf Next ReDim $aOut[$aOut[0] + 1] Return $aOut EndFunc My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions
step Posted August 3, 2005 Author Posted August 3, 2005 Lookin pretty good! and i understand it! thanks blindwig, it looks pretty impressive! i'm still working my way thru the code. many thanks
blindwig Posted August 3, 2005 Posted August 3, 2005 (edited) Let me add some comments then:Func FindMissing($aIn, $iBase) ;setup for 0-based or 1-based arrays If $iBase Then $iBase = 1 Local $Max =$aIn[0] Else $iBase = 0 $Max = UBound($aIn) - 1 EndIf ;sort the array _ArraySort($aIn, 0, $iBase) ;create an output array that will contain the list of missing numbers Local $aOut[$aIn[$Max] - $aIn[$iBase] + 2], $i, $j ;Loop through the input array For $i = $iBase to $Max - 1 ;check that each number is 1 less than the next number If $aIn[$i] + 1 <> $aIn[$i + 1] Then ;loop through the missing numbers For $j = $aIn[$i] + 1 to $aIn[$i + 1] - 1 ;increase the size of the list of missing numbers $aOut[0] = $aOut[0] + 1 ;add this number to the list of missing numbers $aOut[$aOut[0]] = $j Next EndIf Next ;trim the size of the list of missing numbers to actual size ReDim $aOut[$aOut[0] + 1] ;return the list of missing numbers Return $aOut EndFuncEdit: Fixed some code bugs handling 0- vs 1-based arrays Edited August 3, 2005 by blindwig My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now