List-1 List-2 one one two three three three four four five six six seven eight eighttt nine nine
比較兩個列表時展望輸出在Perl
one | one PASS two | * FAIL MISSING three | three PASS * | three FAIL EXTRA four | four PASS five | * FAIL MISSING six | six PASS * | seven FAIL EXTRA eight | eighttt FAIL INVALID nine | nine PASS
其實從我目前的解決方案回報是兩個修改單的參考和參照「找到額外的,丟失的,無效的字符串失敗「列表描述失敗的索引爲」無失敗「,」失蹤「,」額外「或」無效「,這也是(明顯)良好的輸出。
我目前的解決辦法是:
sub compare {
local $thisfound = shift;
local $thatfound = shift;
local @thisorig = @{ $thisfound };
local @thatorig = @{ $thatfound };
local $best = 9999;
foreach $n (1..6) {
local $diff = 0;
local @thisfound = @thisorig;
local @thatfound = @thatorig;
local @fail =();
for (local $i=0;$i<scalar(@thisfound) || $i<scalar(@thatfound);$i++) {
if($thisfound[$i] eq $thatfound[$i]) {
$fail[$i] = 'NO_FAIL';
next;
}
if($n == 1) { # 1 2 3
next unless __compare_missing__();
next unless __compare_extra__();
next unless __compare_invalid__();
} elsif($n == 2) { # 1 3 2
next unless __compare_missing__();
next unless __compare_invalid__();
next unless __compare_extra__();
} elsif($n == 3) { # 2 1 3
next unless __compare_extra__();
next unless __compare_missing__();
next unless __compare_invalid__();
} elsif($n == 4) { # 2 3 1
next unless __compare_extra__();
next unless __compare_invalid__();
next unless __compare_missing__();
} elsif($n == 5) { # 3 1 2
next unless __compare_invalid__();
next unless __compare_missing__();
next unless __compare_extra__();
} elsif($n == 6) { # 3 2 1
next unless __compare_invalid__();
next unless __compare_extra__();
next unless __compare_missing__();
}
push @fail,'INVALID';
$diff += 1;
}
if ($diff<$best) {
$best = $diff;
@thisbest = @thisfound;
@thatbest = @thatfound;
@failbest = @fail;
}
}
return (\@thisbest,\@thatbest,\@failbest)
}
sub __compare_missing__ {
my $j;
### Does that command match a later this command? ###
### If so most likely a MISSING command ###
for($j=$i+1;$j<scalar(@thisfound);$j++) {
if($thisfound[$j] eq $thatfound[$i]) {
$diff += $j-$i;
for ($i..$j-1) { push(@fail,'MISSING'); }
@end = @thatfound[$i..$#thatfound];
@thatfound = @thatfound[0..$i-1];
for ($i..$j-1) { push(@thatfound,'*'); }
push(@thatfound,@end);
$i=$j-1;
last;
}
}
$j == scalar(@thisfound);
}
sub __compare_extra__ {
my $j;
### Does this command match a later that command? ###
### If so, most likely an EXTRA command ###
for($j=$i+1;$j<scalar(@thatfound);$j++) {
if($thatfound[$j] eq $thisfound[$i]) {
$diff += $j-$i;
for ($i..$j-1) { push(@fail,'EXTRA'); }
@end = @thisfound[$i..$#thisfound];
@thisfound = @thisfound[0..$i-1];
for ($i..$j-1) { push (@thisfound,'*'); }
push(@thisfound,@end);
$i=$j-1;
last;
}
}
$j == scalar(@thatfound);
}
sub __compare_invalid__ {
my $j;
### Do later commands match? ###
### If so most likely an INVALID command ###
for($j=$i+1;$j<scalar(@thisfound);$j++) {
if($thisfound[$j] eq $thatfound[$j]) {
$diff += $j-$i;
for ($i..$j-1) { push(@fail,'INVALID'); }
$i=$j-1;
last;
}
}
$j == scalar(@thisfound);
}
但是,這並不完美......誰願意簡化和改進?具體而言...在單個數據集內,一個搜索順序對於一個子集更好,另一個順序對於不同子集更好。
根據你的意見,我認爲你有一些你還沒有解釋的其他要求。也許你可以告訴我們你在做什麼。 – 2010-01-11 19:59:51
這是一個有趣的問題,但在我嘗試其他答案之前,我想確保我正在處理正確的問題。在List1中,您有一系列的項目。在List2中,您希望看到相同的順序(相同順序的相同項目)。 你想知道什麼時候該序列搞砸了。除了不應該存在的意外項目外,您還想知道什麼時候應該存在的項目缺失,何時重複預期項目,*和*,您沒有指定,當任何預期項目出現順序錯亂時。 這是手頭的問題嗎? – 2010-01-11 21:15:33
對不起,昨晚走了......但我最終實現了levenshtein的距離,然後回到桌子......我會發布最終的解決方案 我可能會調查換位,因爲我已經找到了我會喜歡識別這些場景...... – 2010-01-12 15:09:26