Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
54
Task/Palindrome-detection/CLU/palindrome-detection.clu
Normal file
54
Task/Palindrome-detection/CLU/palindrome-detection.clu
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
% Reverse a string
|
||||
str_reverse = proc (s: string) returns (string)
|
||||
chs: array[char] := array[char]$predict(0, string$size(s))
|
||||
for c: char in string$chars(s) do
|
||||
array[char]$addl(chs, c)
|
||||
end
|
||||
return (string$ac2s(chs))
|
||||
end str_reverse
|
||||
|
||||
% 'Normalize' a string (remove everything but letters and make uppercase)
|
||||
normalize = proc (s: string) returns (string)
|
||||
chs: array[char] := array[char]$predict(0, string$size(s))
|
||||
for c: char in string$chars(s) do
|
||||
if c>='a' cand c<='z' then
|
||||
c := char$i2c(char$c2i(c) - 32)
|
||||
end
|
||||
if c>='A' cand c<='Z' then
|
||||
array[char]$addh(chs, c)
|
||||
end
|
||||
end
|
||||
return (string$ac2s(chs))
|
||||
end normalize
|
||||
|
||||
% Check if a string is an exact palindrome
|
||||
palindrome = proc (s: string) returns (bool)
|
||||
return (s = str_reverse(s))
|
||||
end palindrome
|
||||
|
||||
% Check if a string is an inexact palindrome
|
||||
inexact_palindrome = proc (s: string) returns (bool)
|
||||
return (palindrome(normalize(s)))
|
||||
end inexact_palindrome
|
||||
|
||||
% Test cases
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
tests: array[string] := array[string]$[
|
||||
"rotor", "racecar", "RACEcar", "level", "rosetta",
|
||||
"A man, a plan, a canal: Panama",
|
||||
"Egad, a base tone denotes a bad age",
|
||||
"This is not a palindrome"
|
||||
]
|
||||
|
||||
for test: string in array[string]$elements(tests) do
|
||||
stream$puts(po, "\"" || test || "\": ")
|
||||
if palindrome(test) then
|
||||
stream$putl(po, "exact palindrome")
|
||||
elseif inexact_palindrome(test) then
|
||||
stream$putl(po, "inexact palindrome")
|
||||
else
|
||||
stream$putl(po, "not a palindrome")
|
||||
end
|
||||
end
|
||||
end start_up
|
||||
Loading…
Add table
Add a link
Reference in a new issue