Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
# Create an m x n matrix
|
||||
def matrix(m; n; init):
|
||||
if m == 0 then []
|
||||
elif m == 1 then [range(0;n) | init]
|
||||
elif m > 0 then
|
||||
matrix(1;n;init) as $row
|
||||
| [range(0;m) | $row ]
|
||||
else error("matrix\(m);_;_) invalid")
|
||||
end;
|
||||
|
||||
def set(i;j; value):
|
||||
setpath([i,j]; value);
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
def lcs(a; b):
|
||||
matrix(a|length; b|length; 0) as $lengths
|
||||
# state: [ $lengths, greatestLength, answer ]
|
||||
| [$lengths, 0]
|
||||
| reduce range(0; a|length) as $i
|
||||
(.;
|
||||
reduce range(0; b|length) as $j
|
||||
(.;
|
||||
if a[$i:$i+1] == b[$j:$j+1] then
|
||||
(if $i == 0 or $j == 0 then 1
|
||||
else .[0][$i-1][$j-1] + 1
|
||||
end) as $x
|
||||
| .[0] |= set($i; $j; $x)
|
||||
| if $x > .[1] then
|
||||
.[1] = $x
|
||||
| .[2] = a[1+$i - $x : 1+$i] # output
|
||||
else .
|
||||
end
|
||||
else .
|
||||
end )) | .[2];
|
||||
|
|
@ -0,0 +1 @@
|
|||
lcs("thisisatest"; "testing123testing")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ jq -n -f Longest_common_substring.jq
|
||||
"test"
|
||||
Loading…
Add table
Add a link
Reference in a new issue