Check dates if palindrome

5 vues (au cours des 30 derniers jours)
Delya Ochirova
Delya Ochirova le 18 Nov 2022
Commenté : Jan le 21 Nov 2022
sum=0;
n1=input('Please enter first date:');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
n2=input('Please enter second date:');
x=n1:n2;
for i=n1:n2
if (n1>0)
r=round(rem(n1,10)-0.1);
sum=sum*10+r;
n1=round((n1/10)-0.1);
c1=x-sum;
elseif (n2>0)
r=round(rem(n2,10)-0.1);
sum=sum*10+r;
n2=round((n2/10)-0.1);
c2=x-sum;
end
end
Im trying to write the code which is checking palindrome in the interval of dates. how can i put the limits. and then it should print all palindromes one after other.
  4 commentaires
Jan
Jan le 18 Nov 2022
Modifié(e) : Jan le 18 Nov 2022
@Delya Ochirova: What do you call "a date" and what are typical inputs of this code? The input() command is not a good choice to explain a problem. Prefer posting explicit example data.
Avoid to use "sum" as a name of a variable, because this causes troubles, if you want to use the function with the same name later.
Jan
Jan le 18 Nov 2022
Modifié(e) : Jan le 18 Nov 2022
Okay. How yould you use input() to get something like "01.01.1900" ? What do you call a palindrome of e.g. "01.01.1900"? "0091.10.10" is not an option, so please define exactly, what you want.
Because I dared to guess, what you want, I've wasted some time already withwriting a not matching answer.

Connectez-vous pour commenter.

Réponses (3)

KSSV
KSSV le 18 Nov 2022
Modifié(e) : KSSV le 18 Nov 2022
t = datetime(2023,3,20) ;
t.Format = 'Mddyy' ;
if isPalindrome(char(t))
fprintf('%s is a Palindrome\n',char(t))
end
32023 is a Palindrome
function output = isPalindrome(yourString)
reverseString = flip(yourString) ;
if strcmp(yourString,reverseString)
output = true;
else
output = false;
end
end
  3 commentaires
KSSV
KSSV le 18 Nov 2022
@Jan Aboslutely yes. I missed this.
Jan
Jan le 18 Nov 2022
Modifié(e) : Jan le 18 Nov 2022
The pattern:
if condition
result = true;
else
result = false;
end
can be simplified in general to:
result = condition;
which is even faster.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 18 Nov 2022
Are you considering month/day/year separators to be included in the string? Try this where I do it both ways.
% Get user's date:
date1 = input('Please enter first date:', 's');
% Now see if it's a palindrome leaving the separators in.
if isequal(date1, flip(date1))
fprintf('"%s" is a palindrome date with using separators.\n', date1);
else
fprintf('"%s" is not a palindrome date with using separators.\n', date1);
end
% Now see if it's a palindrome with numbers only,
% ignoring the separators.
pat = digitsPattern;
numbersOnly = join(extract(date1, pat));
numbersOnly = strrep(numbersOnly{1}, ' ', '');
if isequal(numbersOnly, flip(numbersOnly))
fprintf('"%s" is a palindrome date without using separators because it is %s.\n', date1, numbersOnly);
else
fprintf('"%s" is not a palindrome date without using separators because it is %s.\n', date1, numbersOnly);
end
Here are some examples:
Please enter first date:2/2/2022
"2/2/2022" is not a palindrome date with using separators.
"2/2/2022" is not a palindrome date without using separators because it is 222022.
Please enter first date:1/11/11
"1/11/11" is not a palindrome date with using separators.
"1/11/11" is a palindrome date without using separators because it is 11111.

Delya Ochirova
Delya Ochirova le 20 Nov 2022
close all
clear
clc
t1=input('First date:');
t2=input('Second date:');
format='ddmmyyyy';
n1=datenum(t1,format);
n2=datenum(t2,format);
for i=n1:n2
n3=datestr(i,format);
if n3==fliplr(n3)
fprintf ('Palindrome dates:%s\n', n3)
end
end
i dont understand why its not working.
  1 commentaire
Jan
Jan le 21 Nov 2022
If it does not work, this is not an answer.
I asked you 3 days ago, what you call a palindrom e.g. for the input "01.01.1900". It would be much easier to help you, if you mention at least, what you exactly want. Posting some code, which does not work, is not useful.
The readers cannot know, what you input as n1 and n2.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by