Longest palindromic substrings: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(4 intermediate revisions by 4 users not shown)
Line 299:
x > []
the abbatial palace > ["abba"]
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ reverse s$ .
a$[] = strchars s$
for i = 1 to len a$[] div 2
swap a$[i] a$[len a$[] - i + 1]
.
return strjoin a$[]
.
func palin s$ .
if s$ = reverse s$
return 1
.
return 0
.
func$ lpali st$ .
for n = 1 to len st$ - 1
for m = n + 1 to len st$
sub$ = substr st$ n (m - n)
if palin sub$ = 1
if len sub$ > len max$
max$ = sub$
.
.
.
.
return max$
.
for s$ in [ "three old rotators" "never reverse" "stable was I ere I saw elbatrosses" "abracadabra" "drome" "the abbatial palace" ]
print lpali s$
.
</syntaxhighlight>
{{out}}
<pre>
rotator
ever reve
table was I ere I saw elbat
aca
d
abba
</pre>
 
Line 337 ⟶ 379:
A longest palindromic substring of "" is ""
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Function isPalindrome(s As String) As Integer
For i As Integer = 1 To Len(s) / 2
If Mid(s, i, 1) <> Mid(s, Len(s) - i + 1, 1) Then Return False
Next i
Return True
End Function
 
Sub LongestPalindrome(s As String)
Dim As String substr, longest = ""
Dim As Integer i, j
For i = 1 To Len(s)
For j = i To Len(s)
substr = Mid(s, i, j - i + 1)
If isPalindrome(substr) Andalso Len(substr) > Len(longest) Then longest = substr
Next j
Next i
Print "The longest palindromic substring is/are: "
For i = 1 To Len(s)
For j = i To Len(s)
substr = Mid(s, i, j - i + 1)
If IsPalindrome(substr) Andalso Len(substr) = Len(longest) Andalso Len(substr) > 2 Then Print substr; " ";
Next j
Next i
If Len(longest) <= 2 Then Print "<no palindromic substring of two of more letters found>"
End Sub
 
Dim s As String
Input "Enter a string: ", s
 
LongestPalindrome(s)
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Line 689 ⟶ 766:
tantôt → tôt
étêté → étêté</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program FindLongestPalindrome;
 
uses
SysUtils,strutils;
 
const
arr: array of string = ('three old rotators', 'never reverse', 'stable was I ere I saw elbatrosses', 'abracadabra', 'drome', 'the abbatial palace', '');
 
var
st, longestPalindrome, dummy: string;
i, j, longest: integer;
 
begin
for st in arr do
begin
longest := 0;
longestPalindrome := '';
for i := 1 to Length(st) do
begin
for j := Length(st) downto i do
begin
dummy := Copy(st, i, j - i + 1);
if (j - i + 1 > longest) and (dummy = ReverseString(dummy)) then
begin
longest := j - i + 1;
longestPalindrome := dummy;
end;
end;
end;
WriteLn(Format('%-35s -> %s', [st, longestPalindrome]));
end;
end.
 
</syntaxhighlight>
{{out}}
<pre>
three old rotators -> rotator
never reverse -> ever reve
stable was I ere I saw elbatrosses -> table was I ere I saw elbat
abracadabra -> aca
drome -> d
the abbatial palace -> abba
->
</pre>
 
=={{header|Perl}}==
The short one - find all palindromes with one regex.
<syntaxhighlight lang="perl">#!/usr/bin/perluse strict;
 
use strict; # https://rosettacode.org/wiki/Longest_palindromic_substrings
use warnings;
 
Line 716 ⟶ 839:
Longest Palindrome For abaracadabra = aba ara aca ada
</pre>
The faster one - does the million digits of Pi in under half a second.
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
The faster one - does the million digits of Pi in under half a second.
use strict; # https://rosettacode.org/wiki/Longest_palindromic_substrings
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'bitwise';
 
#@ARGV = 'pi.dat'; # uncomment to use this file or add filename to command line
Line 738 ⟶ 861:
my $right = substr $forward, $i, $range;
my $left = substr $backward, $length - $i, $range;
( $right ^. $left ) =~ /^\0\0+/ and # evens
($len = 2 * length $&) >= $#best and
$best[ $len ]{substr $forward, $i - length $&, $len}++;
( $right ^. "\0" . $left ) =~ /^.(\0+)/ and # odds
($len = 1 + 2 * length $1) >= $#best and
$best[ $len ]{substr $forward, $i - length $1, $len}++;
Line 1,176 ⟶ 1,299:
 
The Phix entry examples have been used.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./fmt" for Fmt
 
var longestPalSubstring = Fn.new { |s|
2,056

edits