Longest common suffix: Difference between revisions
Content deleted Content added
→{{header|Python}}: Added a draft in Python |
|||
Line 340: | Line 340: | ||
{""} ==> "" |
{""} ==> "" |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
Pending a fuller task statement and some test samples: |
|||
{{works with|Python|3}} |
|||
<lang python>'''Longest common suffix''' |
|||
from itertools import takewhile |
|||
# longestCommonSuffix :: [String] -> String |
|||
def longestCommonSuffix(xs): |
|||
'''Longest suffix shared by all |
|||
strings in xs. |
|||
''' |
|||
def p(cs): |
|||
h = cs[0] |
|||
return all(h == c for c in cs[1:]) |
|||
return ''.join(reversed([ |
|||
x[0] for x in takewhile( |
|||
p, |
|||
zip(*(reversed(x) for x in xs)) |
|||
) |
|||
])) |
|||
# -------------------------- TEST -------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Test''' |
|||
samples = [ |
|||
[ |
|||
"Sunday", "Monday", "Tuesday", "Wednesday", |
|||
"Thursday", "Friday", "Saturday" |
|||
], [ |
|||
"Sondag", "Maandag", "Dinsdag", "Woensdag", |
|||
"Donderdag", "Vrydag", "Saterdag" |
|||
] |
|||
] |
|||
for xs in samples: |
|||
print( |
|||
longestCommonSuffix(xs) |
|||
) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>day |
|||
dag</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |