Longest common subsequence: Difference between revisions

Content added Content deleted
(Logo)
m (→‎{{header|Tcl}}: Small fixes, better organization)
Line 481: Line 481:
=={{header|Tcl}}==
=={{header|Tcl}}==
Both solutions translated from the Java.
Both solutions translated from the Java.
===Recursive===
{{works with|Tcl|8.5}}
<lang tcl>proc r_lcs {a b} {
'''recursive'''
<lang tcl>roc r_lcs {a b} {
if {$a eq "" || $b eq ""} {return ""}
if {$a eq "" || $b eq ""} {return ""}
set a_ [string range $a 1 end]
set a_ [string range $a 1 end]
Line 494: Line 493:
return [expr {[string length $x] > [string length $y] ? $x :$y}]
return [expr {[string length $x] > [string length $y] ? $x :$y}]
}
}
</lang>
}</lang>
===Dynamic===
'''dynamic'''
{{works with|Tcl|8.5}}
<lang tcl>package require Tcl 8.5
<lang tcl>package require Tcl 8.5
namespace import ::tcl::mathop::+
namespace import ::tcl::mathop::+
Line 535: Line 535:
return [string reverse $result]
return [string reverse $result]
}</lang>
}</lang>
Performance:
===Performance Comparison===
<pre>% time {d_lcs thisisatest testing123testing} 10
<lang tcl>% time {d_lcs thisisatest testing123testing} 10
637.5 microseconds per iteration
637.5 microseconds per iteration
% time {r_lcs thisisatest testing123testing} 10
% time {r_lcs thisisatest testing123testing} 10
1275566.8 microseconds per iteration</pre>
1275566.8 microseconds per iteration</lang>