Main Area and Open Discussion > General Software Discussion
How can I find approximate matches of numbers?
KodeZwerg:
Well, he wants to throw in a %match, so that wouldn't do it.-wraith808 (July 12, 2018, 11:34 PM)
--- End quote ---
Now i've understood, thank you for explaining to me!
Here is theory on how to do that, simple range check.
--- --- - checker code
function IsInRange(MinValue, MaxValue: double): Boolean;
var
Count: Integer;
begin
Result := False;
for count := 0 to BigList.NumberOfItems -1 do
if ((BigList.Items[Count].Value >= MinValue) or (BigList.Items[Count].Value <= MaxValue)) then
begin
Result := True;
Exit;
end;
end;
- maincode
for Count := 0 to Masterlist.NumberOfItems -1 do
begin
MinValue := Calculate( MasterList.Items[Count].Value -5% )
MaxValue := Calculate( MasterList.Items[Count].Value +5% )
if IsInRange(MinValue, MaxValue) then
CompareListResults.AddNewItem := Count;
end;
In that sample the MasterList should be the List with less Items than BigList.
I hope you now get an Idea on how to solve this.
Since its just proto-code, it give only first match from criteria MinValue and MaxValue back.
Edited this way it will grab all Matches from BigList
--- --- - checker code
procedure SaveRanges(MinValue, MaxValue: double; var SavedList: TList);
var
Count: Integer;
begin
Result := False;
for count := 0 to BigList.NumberOfItems -1 do
if ((BigList.Items[Count].Value >= MinValue) or (BigList.Items[Count].Value <= MaxValue)) then
SavedList.Items.Add( IntToStr(Count) );
end;
- maincode
NewList := TList.Create();
for Count := 0 to Masterlist.NumberOfItems -1 do
begin
MinValue := Calculate( MasterList.Items[Count].Value -5% )
MaxValue := Calculate( MasterList.Items[Count].Value +5% )
SaveRanges(MinValue, MaxValue, NewList);
end;
This sample would hold all matches in NewList.
Edited this way it will grab all Matches from BigList and Save callers Index within
--- --- - checker code
procedure SaveRanges(const MinValue: double; const MaxValue: double; const CallerIndex: Integer; var SavedList: TList);
var
Count: Integer;
begin
Result := False;
for count := 0 to BigList.NumberOfItems -1 do
if ((BigList.Items[Count].Value >= MinValue) or (BigList.Items[Count].Value <= MaxValue)) then
SavedList.Items.Add( IntToStr(CallerIndex) + ' found match with ' + IntToStr(Count) );
end;
- maincode
NewList := TList.Create();
for Count := 0 to Masterlist.NumberOfItems -1 do
begin
MinValue := Calculate( MasterList.Items[Count].Value -5% )
MaxValue := Calculate( MasterList.Items[Count].Value +5% )
SaveRanges(MinValue, MaxValue, Count, NewList);
end;
This sample would hold all matches in NewList, ready to use with matches display.
Ps: If you are confused of line "for Count := 0 to Masterlist.NumberOfItems -1 do", adjust this to your Language.
In Delphi Lists Index begin at "0" and NumberOfItems at "1". NumberOfItems "0" would mean Index "-1" (no item exist in List)
4wd:
--- Code: PowerShell ---# Remove test filesRemove-Item .\TestList?.txt# Generate two files containing unique random numbers for comparison1..100 | % {Get-Random -Minimum 95 -Maximum 200 } | Sort -Unique | Out-File .\TestList1.txt -Append1..100 | % {Get-Random -Minimum 95 -Maximum 200 } | Sort -Unique | Out-File .\TestList2.txt -Append # Read the files into an object (array)$list1 = Get-Content .\TestList1.txt$list2 = Get-Content .\TestList2.txt # Initialise match count$total = 0 # For each value in list 1 loop through every value in list 2for ($i = 0; $i -lt $list1.Count; $i++) { for ($j = 0; $j -lt $list2.Count; $j++) { $tmp = $list2[$j]/1 # Simple conversion of string to number if (($tmp -gt ($list1[$i]/1 * 0.95)) -and ($tmp -lt ($list1[$i]/1 * 1.05))) { # Compare value from list 2 to within 5% of value from list 1 $total++ # Increment match count if a match $out = 'Match ' + $total.ToString() + ': ' + $list1[$i] + ' ±5% -> ' + $list2[$j] # Format output Write-Host $out # Write to console Out-File .\TestList3.txt -InputObject $out -Append # Write to file } } # Round we go again} # ... and again # The End
wraith808:
All of these are great solutions. I still maintain that the problem hasn't been defined properly, so they could be for naught. :(
Ath had the right questions in the initial reply. :-\
KodeZwerg:
In my proto-Code i was wrong anyway :o
What i've called BigList should be infact the smaller List = fastest performance.
And the "Result :=" Lines should be removed from all Procedures ^_^
4wd:
I still maintain that the problem hasn't been defined properly, so they could be for naught-wraith808 (July 14, 2018, 11:19 AM)
--- End quote ---
That is always the case.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version