Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,33 @@
|
|||
on LCS(a, b)
|
||||
-- Identify the shorter string. The longest common substring won't be longer than it!
|
||||
set lengthA to a's length
|
||||
set lengthB to b's length
|
||||
if (lengthA < lengthB) then
|
||||
set {shorterString, shorterLength, longerString} to {a, lengthA, b}
|
||||
else
|
||||
set {shorterString, shorterLength, longerString} to {b, lengthB, a}
|
||||
end if
|
||||
|
||||
set longestMatches to {}
|
||||
set longestMatchLength to 0
|
||||
-- Find the longest matching substring starting at each character in the shorter string.
|
||||
repeat with i from 1 to shorterLength
|
||||
repeat with j from shorterLength to i by -1
|
||||
set thisSubstring to text i thru j of shorterString
|
||||
if (longerString contains thisSubstring) then
|
||||
-- Match found. If it's longer than the previously found match, or a new string of the same length, remember it.
|
||||
set matchLength to j - i + 1
|
||||
if (matchLength > longestMatchLength) then
|
||||
set longestMatches to {thisSubstring}
|
||||
set longestMatchLength to matchLength
|
||||
else if ((matchLength = longestMatchLength) and (thisSubstring is not in longestMatches)) then
|
||||
set end of longestMatches to thisSubstring
|
||||
end if
|
||||
-- Don't bother with the match's own substrings.
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end repeat
|
||||
|
||||
return longestMatches
|
||||
end LCS
|
||||
|
|
@ -0,0 +1 @@
|
|||
LCS("thisisatest", "testing123testing")
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"test"}
|
||||
|
|
@ -0,0 +1 @@
|
|||
LCS("thisisthebesttest", "besting123testing")
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"best", "test"}
|
||||
|
|
@ -0,0 +1,180 @@
|
|||
------------------ LONGEST COMMON SUBSTRING ----------------
|
||||
|
||||
-- longestCommon :: Eq a => [a] -> [a] -> [a]
|
||||
on longestCommon(a, b)
|
||||
-- The longest common substring of two given strings.
|
||||
script substrings
|
||||
on |λ|(s)
|
||||
map(my concat, concatMap(my tails, rest of inits(s)))
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
set {xs, ys} to map(substrings, {a, b})
|
||||
maximumBy(comparing(my |length|), intersect(xs, ys))
|
||||
end longestCommon
|
||||
|
||||
|
||||
|
||||
-------------------------- TEST ---------------------------
|
||||
on run
|
||||
longestCommon("testing123testing", "thisisatest")
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS --------------------
|
||||
|
||||
-- comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
on comparing(f)
|
||||
script
|
||||
on |λ|(a, b)
|
||||
tell mReturn(f)
|
||||
set fa to |λ|(a)
|
||||
set fb to |λ|(b)
|
||||
if fa < fb then
|
||||
-1
|
||||
else if fa > fb then
|
||||
1
|
||||
else
|
||||
0
|
||||
end if
|
||||
end tell
|
||||
end |λ|
|
||||
end script
|
||||
end comparing
|
||||
|
||||
|
||||
-- concat :: [String] -> String
|
||||
on concat(xs)
|
||||
script go
|
||||
on |λ|(a, x)
|
||||
a & x
|
||||
end |λ|
|
||||
end script
|
||||
foldl(go, "", xs)
|
||||
end concat
|
||||
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lng to length of xs
|
||||
set acc to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & (|λ|(item i of xs, i, xs))
|
||||
end repeat
|
||||
end tell
|
||||
return acc
|
||||
end concatMap
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- inits :: String -> [String]
|
||||
on inits(xs)
|
||||
script charInit
|
||||
on |λ|(_, i, xs)
|
||||
text 1 thru i of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
{""} & map(charInit, xs)
|
||||
end inits
|
||||
|
||||
|
||||
-- intersect :: (Eq a) => [a] -> [a] -> [a]
|
||||
on intersect(xs, ys)
|
||||
if length of xs < length of ys then
|
||||
set {shorter, longer} to {xs, ys}
|
||||
else
|
||||
set {longer, shorter} to {xs, ys}
|
||||
end if
|
||||
if shorter ≠ {} then
|
||||
set lst to {}
|
||||
repeat with x in shorter
|
||||
if longer contains x then set end of lst to contents of x
|
||||
end repeat
|
||||
lst
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end intersect
|
||||
|
||||
|
||||
-- length :: [a] -> Int
|
||||
on |length|(xs)
|
||||
set c to class of xs
|
||||
if list is c or string is c then
|
||||
length of xs
|
||||
else
|
||||
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
|
||||
end if
|
||||
end |length|
|
||||
|
||||
|
||||
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
on maximumBy(f, xs)
|
||||
set cmp to mReturn(f)
|
||||
script max
|
||||
on |λ|(a, b)
|
||||
if a is missing value or cmp's |λ|(a, b) < 0 then
|
||||
b
|
||||
else
|
||||
a
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(max, missing value, xs)
|
||||
end maximumBy
|
||||
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
-- The list obtained by applying f
|
||||
-- to each element of xs.
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- tails :: String -> [String]
|
||||
on tails(xs)
|
||||
set es to characters of xs
|
||||
script residue
|
||||
on |λ|(_, i)
|
||||
items i thru -1 of es
|
||||
end |λ|
|
||||
end script
|
||||
map(residue, es) & {""}
|
||||
end tails
|
||||
Loading…
Add table
Add a link
Reference in a new issue