Palindrome detection: Difference between revisions

→‎{{header|ed}}: Minor grammatical fix
No edit summary
(→‎{{header|ed}}: Minor grammatical fix)
(9 intermediate revisions by 6 users not shown)
Line 1,911:
;;(equal? (string->list string) (reverse (string->list string)))))
</syntaxhighlight>
 
=={{header|ed}}==
 
A limitation: due to ed having no built-in loops, the part with palindrome beginning/end matching has to be repeated as many times as there are palindrome levels. As a sane default, 15 is used here.
 
<syntaxhighlight lang="sed">
# by Artyom Bologov
H
,p
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
g/^(.)(.+)\1$/s//\2/
v/^(.)(.+)\1$|^.$/s/.*/Not a palindrome!/
g/^.$/s//Palindrome!/
,p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ cat palindrome.ed | ed -lEGs palindrome.input
rotor
racecar
level
rosetta
Palindrome!
Palindrome!
Palindrome!
Not a palindrome!</pre>
 
=={{header|Eiffel}}==
Line 3,259 ⟶ 3,300:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .ispal = fn(.s) len(.s) > 0 and .s == reverse .s
val ispal = fn s:len(s) > 0 and s == reverse(s)
 
val .tests = h{
"": false,
"z": true,
Line 3,278 ⟶ 3,320:
}
 
for .word in sort(keys .(tests)) {
val .foundpal = .ispal(.word)
writeln .word, ": ", .foundpal, if(.foundpal == .tests[.word]: ""; " (FAILED TEST)")
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 4,129 ⟶ 4,172:
writeln('input was not palindrome');
end.</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function IsPalindrome(s: string) := s = s[::-1];
 
begin
Println(IsPalindrome('arozaupalanalapuazora'));
Println(IsPalindrome('abcd'));
end.
</syntaxhighlight>
{{out}}
<pre>
True
False
</pre>
 
=={{header|Perl}}==
Line 5,209 ⟶ 5,267:
'ingirumimusnocteetconsumimurigni palindrome? n:put
</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Test 'rotor'>
<Test 'racecar'>
<Test 'RACEcar'>
<Test 'level'>
<Test 'rosetta'>
<Test 'A man, a plan, a canal: Panama'>
<Test 'Egad, a base tone denotes a bad age'>
<Test 'This is not a palindrome'>;
};
 
Test {
e.W, <Palindrome e.W> <InexactPalindrome e.W>: {
True s.1 = <Prout e.W ': exact palindrome'>;
s.1 True = <Prout e.W ': inexact palindrome'>;
False False = <Prout e.W ': not a palindrome'>;
};
};
 
InexactPalindrome {
e.W = <Palindrome <Filter ('ABCDEFGHIJKLMNOPQRSTUVWXYZ') <Upper e.W>>>;
};
 
Filter {
(e.Keep) = ;
(e.Keep) s.C e.W, e.Keep: {
e.1 s.C e.2 = s.C <Filter (e.Keep) e.W>;
e.1 = <Filter (e.Keep) e.W>;
};
};
 
Palindrome {
= True;
s.C = True;
s.C e.W s.C = <Palindrome e.W>;
e.X = False;
};</syntaxhighlight>
{{out}}
<pre>rotor: exact palindrome
marinus@frankenstein:~/refal$ refc palin && refgo palin
Refal-5 Compiler. Version PZ Jan 25 2024
Copyright: Refal Systems Inc.
rotor: exact palindrome
racecar: exact palindrome
RACEcar: inexact palindrome
level: exact palindrome
rosetta: not a palindrome
A man, a plan, a canal: Panama: inexact palindrome
Egad, a base tone denotes a bad age: inexact palindrome
This is not a palindrome: not a palindrome</pre>
 
=={{header|REXX}}==
Line 5,388 ⟶ 5,498:
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">data "My dog has fleas", "Madam, I'm Adam.", "1 on 1", "In girum imus nocte et consumimur igni"
 
for i = 1 to 4
read w$
print w$;" is ";isPalindrome$(w$);" Palindrome"
next
 
FUNCTIONfunction isPalindrome$(str$)
for i = 1 to len(str$)
a$ = upper$(mid$(str$,i,1))
if (a$ >= "A" and a$ <= "Z") or (a$ >= "0" and a$ <= "9") then b$ = b$ + a$: c$ = a$ + c$
next i
if b$ <> c$ then isPalindrome$ = "not"</syntaxhighlight>
end function</syntaxhighlight>
{{out}}
<pre>My dog has fleas is not Palindrome
Line 5,943 ⟶ 6,054:
Does not ignore spaces.
<syntaxhighlight lang="uiua">≍⇌."tacocat"</syntaxhighlight>
===Extra Credit===
Ignores whitespace, converts A-Z to lowercase, only checks a-z, includes tests.
<syntaxhighlight lang="uiua">IsPal ← ≍⇌.+×32<@a.▽:⟜∊:/⊂+⊙¤"Aa"⇡26
IsPal "A man, a plan, a canal: Panama!"
</syntaxhighlight>
 
=={{header|UNIX Shell}}==
104

edits