Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,8 @@
|
|||
(define $common-seqs
|
||||
(lambda [$xs $ys]
|
||||
(match-all [xs ys] [(list char) (list char)]
|
||||
[[(loop $i [1 $n] <join _ <cons $c_i ...>> _)
|
||||
(loop $i [1 ,n] <join _ <cons ,c_i ...>> _)]
|
||||
(map (lambda [$i] c_i) (between 1 n))])))
|
||||
|
||||
(define $lcs (compose common-seqs rac))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> (lcs "thisisatest" "testing123testing"))
|
||||
"tsitest"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
proc lcs(x, y): string =
|
||||
if x == "" or y == "":
|
||||
return ""
|
||||
|
||||
if x[0] == y[0]:
|
||||
return x[0] & lcs(x[1..x.high], y[1..y.high])
|
||||
|
||||
let a = lcs(x, y[1..y.high])
|
||||
let b = lcs(x[1..x.high], y)
|
||||
result = if a.len > b.len: a else: b
|
||||
|
||||
echo lcs("1234", "1224533324")
|
||||
echo lcs("thisisatest", "testing123testing")
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
proc lcs(a, b): string =
|
||||
var ls = newSeq[seq[int]] a.len+1
|
||||
for i in 0 .. a.len:
|
||||
ls[i].newSeq b.len+1
|
||||
|
||||
for i, x in a:
|
||||
for j, y in b:
|
||||
if x == y:
|
||||
ls[i+1][j+1] = ls[i][j] + 1
|
||||
else:
|
||||
ls[i+1][j+1] = max(ls[i+1][j], ls[i][j+1])
|
||||
|
||||
result = ""
|
||||
var x = a.len
|
||||
var y = b.len
|
||||
while x > 0 and y > 0:
|
||||
if ls[x][y] == ls[x-1][y]:
|
||||
dec x
|
||||
elif ls[x][y] == ls[x][y-1]:
|
||||
dec y
|
||||
else:
|
||||
assert a[x-1] == b[y-1]
|
||||
result = a[x-1] & result
|
||||
dec x
|
||||
dec y
|
||||
|
||||
echo lcs("1234", "1224533324")
|
||||
echo lcs("thisisatest", "testing123testing")
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
see longest("1267834", "1224533324") + nl
|
||||
|
||||
func longest a, b
|
||||
if a = "" or b = "" return "" ok
|
||||
if right(a, 1) = right(b, 1)
|
||||
lcs = longest(left(a, len(a) - 1), left(b, len(b) - 1)) + right(a, 1)
|
||||
return lcs
|
||||
else
|
||||
x1 = longest(a, left(b, len(b) - 1))
|
||||
x2 = longest(left(a, len(a) - 1), b)
|
||||
if len(x1) > len(x2)
|
||||
lcs = x1
|
||||
return lcs
|
||||
else
|
||||
lcs = x2
|
||||
return lcs ok ok
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
func lcs(xstr, ystr) is cached {
|
||||
|
||||
xstr.is_empty && return xstr;
|
||||
ystr.is_empty && return ystr;
|
||||
|
||||
var(x, xs, y, ys) = (xstr.ft(0,0), xstr.ft(1),
|
||||
ystr.ft(0,0), ystr.ft(1));
|
||||
|
||||
if (x == y) {
|
||||
x + lcs(xs, ys)
|
||||
} else {
|
||||
[lcs(xstr, ys), lcs(xs, ystr)].max_by { .len };
|
||||
}
|
||||
}
|
||||
|
||||
say lcs("thisisatest", "testing123testing");
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
func rlcs(s1:String, _ s2:String) -> String {
|
||||
let x = s1.characters.count
|
||||
let y = s2.characters.count
|
||||
|
||||
if x == 0 || y == 0 {
|
||||
return ""
|
||||
} else if s1[s1.startIndex.advancedBy(x - 1)] == s2[s2.startIndex.advancedBy(y - 1)] {
|
||||
return rlcs(s1[s1.startIndex..<s1.startIndex.advancedBy(x - 1)],
|
||||
s2[s2.startIndex..<s2.startIndex.advancedBy(y - 1)]) + String(s1[s1.startIndex.advancedBy(x - 1)])
|
||||
} else {
|
||||
let xstr = rlcs(s1, s2[s2.startIndex..<s2.startIndex.advancedBy(y - 1)])
|
||||
let ystr = rlcs(s1[s1.startIndex..<s1.startIndex.advancedBy(x - 1)], s2)
|
||||
|
||||
return xstr.characters.count > ystr.characters.count ? xstr : ystr
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
func lcs(s1:String, _ s2:String) -> String {
|
||||
var x = s1.characters.count
|
||||
var y = s2.characters.count
|
||||
var lens = Array(count: x + 1, repeatedValue:
|
||||
Array(count: y + 1, repeatedValue: 0))
|
||||
var returnStr = ""
|
||||
|
||||
for i in 0..<x {
|
||||
for j in 0..<y {
|
||||
if s1[s1.startIndex.advancedBy(i)] == s2[s2.startIndex.advancedBy(j)] {
|
||||
lens[i + 1][j + 1] = lens[i][j] + 1
|
||||
} else {
|
||||
lens[i + 1][j + 1] = max(lens[i + 1][j], lens[i][j + 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while x != 0 && y != 0 {
|
||||
if lens[x][y] == lens[x - 1][y] {
|
||||
--x
|
||||
} else if lens[x][y] == lens[x][y - 1] {
|
||||
--y
|
||||
} else {
|
||||
returnStr += String(s1[s1.startIndex.advancedBy(x - 1)])
|
||||
--x
|
||||
--y
|
||||
}
|
||||
}
|
||||
|
||||
return String(returnStr.characters.reverse())
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def lcs(xstr; ystr):
|
||||
if (xstr == "" or ystr == "") then ""
|
||||
else
|
||||
xstr[0:1] as $x
|
||||
| xstr[1:] as $xs
|
||||
| ystr[1:] as $ys
|
||||
| if ($x == ystr[0:1]) then ($x + lcs($xs; $ys))
|
||||
else
|
||||
lcs(xstr; $ys) as $one
|
||||
| lcs($xs; ystr) as $two
|
||||
| if ($one|length) > ($two|length) then $one else $two end
|
||||
end
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
lcs("1234"; "1224533324"),
|
||||
lcs("thisisatest"; "testing123testing")
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
# jq -n -f lcs-recursive.jq
|
||||
"1234"
|
||||
"tsitest"
|
||||
Loading…
Add table
Add a link
Reference in a new issue