Longest common suffix: Difference between revisions
No edit summary |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 24: | Line 24: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Essentially, this REXX version simply reversed the strings, and then finds the longest common ''prefix''. |
|||
<lang rexx>/*REXX program finds the longest common suffix contained in an array of strings. */ |
|||
parse arg z; z= space(z) /*obtain optional arguments from the CL*/ |
|||
if z==''|z=="," then z='baabababc baabc bbbabc' /*Not specified? Then use the default.*/ |
|||
z= space(z); #= words(z) /*#: the number of words in the list. */ |
|||
say 'There are ' # " words in the list: " z |
|||
zr= reverse(z) /*reverse Z, find longest common prefix*/ |
|||
@= word(zr, 1); m= length(@) /*get 1st word in reversed string; len.*/ |
|||
do j=2 to #; x= word(zr, j) /*obtain a word (string) from the list.*/ |
|||
t= compare(@, x) /*compare to the "base" word/string. */ |
|||
if t==1 then do; @=; leave /*A mismatch of strings? Then leave, */ |
|||
end /*no sense in comparing anymore strings*/ |
|||
if t==0 & @==x then t= length(@) + 1 /*Both strings equal? Compute length. */ |
|||
if t>=m then iterate /*T ≥ M? Then it's not longest string.*/ |
|||
m= t - 1; @= left(@, max(0, m) ) /*redefine max length & the base string*/ |
|||
end /*j*/ |
|||
say /*stick a fork in it, we're all done. */ |
|||
if m==0 then say 'There is no common suffix.' |
|||
else say 'The longest common suffix is: ' right( word(z, 1), m)</lang> |
|||
{{out|output|text= when using the default input:}} |
|||
<pre> |
|||
There are 3 words in the list: baabababc baabc bbbabc |
|||
The longest common suffix is: abc |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 08:23, 25 July 2020
- Task
The goal is to write a function to find the longest common suffix string amongst an array of strings.
Julia
<lang julia>function longestcommonsuffix(strings)
n, nmax = 0, minimum(length, strings) nmax == 0 && return "" while n <= nmax && all(s -> s[end-n] == strings[end][end-n], strings) n += 1 end return strings[1][end-n+1:end]
end
println(longestcommonsuffix(["baabababc","baabc","bbbabc"])) println(longestcommonsuffix(["baabababc","baabc","bbbazc"]))
println(longestcommonsuffix([""]))</lang>
- Output:
abc c
REXX
Essentially, this REXX version simply reversed the strings, and then finds the longest common prefix. <lang rexx>/*REXX program finds the longest common suffix contained in an array of strings. */ parse arg z; z= space(z) /*obtain optional arguments from the CL*/ if z==|z=="," then z='baabababc baabc bbbabc' /*Not specified? Then use the default.*/ z= space(z); #= words(z) /*#: the number of words in the list. */ say 'There are ' # " words in the list: " z zr= reverse(z) /*reverse Z, find longest common prefix*/ @= word(zr, 1); m= length(@) /*get 1st word in reversed string; len.*/
do j=2 to #; x= word(zr, j) /*obtain a word (string) from the list.*/ t= compare(@, x) /*compare to the "base" word/string. */ if t==1 then do; @=; leave /*A mismatch of strings? Then leave, */ end /*no sense in comparing anymore strings*/ if t==0 & @==x then t= length(@) + 1 /*Both strings equal? Compute length. */ if t>=m then iterate /*T ≥ M? Then it's not longest string.*/ m= t - 1; @= left(@, max(0, m) ) /*redefine max length & the base string*/ end /*j*/
say /*stick a fork in it, we're all done. */ if m==0 then say 'There is no common suffix.'
else say 'The longest common suffix is: ' right( word(z, 1), m)</lang>
- output when using the default input:
There are 3 words in the list: baabababc baabc bbbabc The longest common suffix is: abc
Ring
<lang ring> load "stdlib.ring"
pre = ["baabababc","baabc","bbbabc"] len = len(pre) lenList = list(len) sub = list(len)
see "Input:" + nl see pre
for n = 1 to len
temp = pre[n] pre[n] = rever(temp)
next
for n = 1 to len
lenList[n] = len(pre[n])
next
lenList = sort(lenList) lenMax = lenList[1]
for m = 1 to lenMax
check = 0 sub1 = substr(pre[1],1,m) sub2 = substr(pre[2],1,m) sub3 = substr(pre[3],1,m) if sub1 = sub2 and sub2 = sub3 check = 1 ok if check = 1 longest = m ok
next
longPrefix = substr(pre[1],1,longest) longPrefix = rever(longPrefix)
see "Longest common suffix = " + longPrefix + nl
func rever(cstr)
cStr2 = "" for x = len(cStr) to 1 step -1 cStr2 = cStr2 + cStr[x] next return cStr2
</lang>
- Output:
Input: baabababc baabc bbbabc Longest common suffix = abc