recursive function to check a Palindrome

function out=palindrome(x)
n=numel(x);
if n==1
out=true;
else
Can anyone continue from here?

Respuestas (4)

John D'Errico
John D'Errico el 11 de Sept. de 2020

4 votos

Why not try? I won't write it for you, as the entire problem is to make you think. I'll give you one hint, and I will hope nobody does the rest of your homework for you.
A vector is a palindrome if the first and last elements are the same, AND...
.... what else? That is, what information do you need about the rest of the vector? Is there some way you can treat this in a recursive way?
This is your homework, not ours, remember.

1 comentario

Harsh Mittal
Harsh Mittal el 20 de Oct. de 2020
I tried writing the code but i am getting error as "MATLAB:TooManyOutputs" , can anyone help me resolve this error, i recieved this error while submiting my code

Iniciar sesión para comentar.

xin yi leow
xin yi leow el 25 de En. de 2021
function t=palindrome(char)
if length(char)==2
if char(1)==char(2)
t=true
else
t=false
end
elseif length(char)==1
t=true
else
if char(1)==char(end)
char(1)=[];
char(end)=[]
t=palindrome(char)
else
t=false
end
end
end
Johannes Hougaard
Johannes Hougaard el 10 de Sept. de 2020
There is no need to make it recursive or to look for multiple numel's
function out = palindrom(x)
x = upper(char(varargin{1})); % to account for inputs being UPPER/lower case and/or a string rather than a char
n = length(x);
iseven = rem(n,2) == 0;
middle = ceil(n/2);
if ~iseven
out = all(x(1:middle) == fliplr(x(middle:end)));
else
out = all(x(1:middle) == fliplr(x(middle+1:end)));
end
end

4 comentarios

It's probably a requirement for his homework.
But your function could be done simply like this:
palindrome = isequal(lower(s), fliplr(lower(s)))
No need to worry about length and which character is in the middle.
Johannes Hougaard
Johannes Hougaard el 11 de Sept. de 2020
Valid point - classic example of overthinking a simple problem :S
Urmish Haribhakti
Urmish Haribhakti el 11 de Sept. de 2020
Correct. Obviously, very simple to do it normally. Any idea on how to make it recursive?
Selvakumar T
Selvakumar T el 25 de Sept. de 2020
function Result = palindrome(t) if length(t) <= 1 Result= true; else Result= (t(1) == t(end) &&palindrome(txt(2:end-1))); end end

Iniciar sesión para comentar.

Wai Han
Wai Han el 18 de Oct. de 2020
Editada: Wai Han el 18 de Oct. de 2020
Hello Urmish Haribhakti,
I suggest you to try your best like John D'Errico said.
But somehow, I can help you out by telling you the concept, (maybe a pseudocode).
% The input is a string which is in term a char array.
% Firstly, if the length of the string is 1, or isempty!! then the function should return logical 1 (true).
% elseif length ~= 1,check the first and last value of the vector,
% if first == last
% recall the function by removing those two values. for example, vector(2:end-1)
% Lastly, if the first and last value are not equal, the your output should be logical 0 (false).
I hope this can help you out!

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Sept. de 2020

Respondida:

el 25 de En. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by