Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not

1 vue (au cours des 30 derniers jours)
Ajith Thomas
Ajith Thomas le 29 Juin 2019
Clôturé : Image Analyst le 12 Avr 2021
unction valid=valid_date(year,month,date)
if nargin==3
valid1=true;
else valid=false;
return
end
v1=[year]; v2=[month]; v3=[date];
if isscalar(v1)==true && isscalar(v2)==true && isscalar(v3)==true
valid2=true;
else valid=false;
return
end
if year>0 && 0<month && month<=12 && 0<date && date<=31
valid3=true;
else valid=false;
return
end
a=year/4; b=year/100; c=year/400;
if rem(year,4)==0 && rem(year,100)~=0
valid4=true;
else valid4=false;
end
if rem(year,100)==0 && rem(year,400)~=0
valid5=true;
else valid5=false;
end
if rem(year,400)==0
valid4=true;
else valid4=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=29) || (month==4||6||9||11 && date<=30) && valid4==true && valid5==false
valid6=true;
else valid6=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=28) || (month==4||6||9||11 && date<=30) && valid5==true && valid4==false
valid7=true;
else
valid7=false;
end
if valid1==true && valid2==true && valid3==true && valid6==true
valid=true;
elseif valid1==true &&valid2==true && valid3==true && valid7==true
valid=true;
else
valid=false;
return
end
why this code is not working for 2018/4/31 and 2003/2/29? and other random dates. but is works for non scalar and random leap years
  8 commentaires
Rik
Rik le 4 Fév 2021
Did you use the debugger to run your code line by line for this input?
Panagiotis Papias
Panagiotis Papias le 8 Fév 2021
Tbh, no you are right i have to get more familiar with the debugging. However, i followed sb's else instructions and i managed to solve the assignment 1 hr later. I think i got it what i did wrong

Réponses (11)

SANTOSH KAMBLE
SANTOSH KAMBLE le 3 Mai 2020
Modifié(e) : SANTOSH KAMBLE le 4 Mai 2020
function [valid]=valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif y>=1 && m==3 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==4 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==5 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==6 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==7 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==8 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==9 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==10 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==11 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==12 && d>=1 && d<=31
valid=true;
else
valid=false;
end
end
  15 commentaires
anil Reddy
anil Reddy le 21 Oct 2020
if i take (0<month<=12) in place of ((12>=month) && (month>0)) why am i getting mistake
Rik
Rik le 21 Oct 2020
Because that is not how Matlab operations work. The explanation provided by mlint explains it:

per isakson
per isakson le 29 Juil 2020
Modifié(e) : per isakson le 29 Juil 2020
Would this function pass?
function [ valid, dt ] = valid_date( y, m, d )
% The names of the input arguments, (year,month,day), are they
% mandatory? Are these names chosen to shadow functions in the
% finance toolbox?
try
% datetime is "smart". Doc says: "Each element of DateVector
% should be a positive or negative integer value [...]. If an
% element falls outside the conventional range, datetime adjusts
% both that date vector element and the previous element."
dt = datetime( y, m, d );
% If datetime didn't adjust any element the input is a
% valid date.
% valid = all( [ year(dt)==y, month(dt)==m, day(dt)==d ] );
vec = datevec( dt );
valid = all( [ vec(1)==y, vec(2)==m, vec(3)==d ] );
catch
% datetime throws an exception when not all input values are
% integers
dt = datetime.empty;
valid = false;
end
end
  1 commentaire
Rik
Rik le 13 Août 2020
I would hope so. As an instructor I would applaud lateral thinking like this. Just as I would accept code where someone did this:
for n=1%use a loop because it is a requirement
output=sum(data,2);
end

Iccu OUMOUACHA
Iccu OUMOUACHA le 25 Mai 2020
function valid=valid_date(year, month, day)
if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0
valid=false;
return
end
if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))
valid=true;
elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))
valid=true;
elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)
valid=true;
elseif month==2 && ismember(day, 1:28)
valid=true;
else
valid=false;
return
end
end
it works in Matlab, when I test a non-scalar inputs, it return false. But here it doesn't work!!!!! ((Assessment 2))
What is the problem?
  4 commentaires
Rik
Rik le 25 Mai 2020
Then you should change your code.
If you think this comment is unhelpful: show the code you used to ensure the output is false for non-scalar inputs. That is the only way someone will be able to help you.
Chandan Kumar
Chandan Kumar le 3 Mar 2021
looks like you didnt verifyng the input is scalar or not

Capulus_love
Capulus_love le 11 Août 2020
Modifié(e) : per isakson le 11 Août 2020
function x = valid_date(year,month,day)
if nargin == 3
if isscalar(year) && isscalar(month) && isscalar(day)
if month > 0 && month <= 12 && day >= 0 && year > 0
if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 && day <= 31)...
|| (month == 2 && day <= 29) && (month == 4 || 6 || 9 || 11 && day <= 30)
c0 = mod(year,4)
c1 = mod(year,100)
c2 = mod(year,400)
if (c0 == 0 && c1 ~=0) || (c1 == 0 && c2 ~= 0) || c2 == 0
x = 'true'
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
end
why this code not working? :(
  1 commentaire
Rik
Rik le 11 Août 2020
Let's try some cases that will likely point us to an issue:
valid_date(2020,2,29) %returns true
valid_date(2021,2,29) %returns false
valid_date(2021,2,28) %returns false
That last one is a problem. Can you follow the flow of your code where it should mark Feb 29 as valid only in leap years, and the rest of Feb as valid every year?
Also, your code returns the value as a char array, not a logical.

Adonis Thirafi Hugo Mafaza
Why is my code not working?
  1 commentaire
Rik
Rik le 30 Oct 2020
Because you didn't use the debugging tools to run your code line by line.

QueenX
QueenX le 31 Oct 2020
Modifié(e) : QueenX le 31 Oct 2020
I did spend whole noon to make it work. Even though it is quite long, hope it is useful for someone.
function valid = valid_date(year, month, day)
if ~isscalar(year) || year ~= fix(year)||year<=0
valid = false;
elseif ~isscalar(month) || month >12 || month<=0 || month ~= fix(month)
valid = false;
elseif ~isscalar(day) || day >31 || day ~= fix(day)|| day<=0
valid = false;
elseif day >29 && month == 2 && rem(year,4) == 0 && rem(year,100)~=0 %leap year
valid = false;
elseif day>29 && month == 2 && rem(year,400) == 0 %leap year
valid =false;
elseif day>30 && (month == 4 ||month == 6||month == 9 ||month == 11)
valid =false;
elseif day>28 && month==2 && rem(year,100) == 0 && rem(year,4)==0 && rem(year,400)~=0 %non leap year
valid =false;
elseif day>28 && month==2 && rem(year,4)~=0 && rem(year,400)~=0 %non leap year
valid =false;
else
valid = true;
end
end
  2 commentaires
Rik
Rik le 31 Oct 2020
It is a good exercise to write a function like this, but why did you post this? What does it add to the previous answers?
QueenX
QueenX le 1 Nov 2020
I simply think that there are a lot of ways to solve this code. Just want to share my view. Is there any problem? btw

Amrut Umrankar
Amrut Umrankar le 15 Nov 2020
Modifié(e) : Amrut Umrankar le 15 Nov 2020
function valid = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day) || year ~=fix(year) || month ~=fix(month) || day ~=fix(day)
valid = false;
return;
end
if month <= 0 || month >= 13 || day <= 0 || year <=0
valid = false;
return;
end
ma =0;
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
ma = 1;
elseif month == 2
ma = 2;
elseif month == 4 || month == 6 || month == 9 || month == 11
ma= 3;
end
if ma == 1
if day >= 32
valid = false;
else
valid = true;
end
end
if ma == 2
if mod( year , 4 )
if day >= 29
valid = false;
else
valid = true;
end
end
if ~mod( year , 4 )
if ~mod(year , 100)
if ~mod(year, 400)
if day >= 30
valid = false;
else
valid = true;
end
else
if day >= 29
valid = false;
else
valid = true;
end
end
else
if day >= 30
valid = false;
else
valid = true;
end
end
end
end
if ma == 3
if day >= 31
valid = false;
else
valid = true;
end
end
  3 commentaires
Amrut Umrankar
Amrut Umrankar le 15 Nov 2020
It is just Another way to solve same problem, As I have used pretty basic coding that's why it is big. It can be optimized.
Rik
Rik le 15 Nov 2020
Big code is not a problem. I think undocumented code is a problem if you want to teach people something. You don't explain what your code is doing. Your choice of variable names also doesn't help: why use ma if you can use something more descriptive like MonthType? I know you are determining if a year is a leap year, but you don't explain that. If someone is inexperienced enough to scroll down all the way to your answer, you can't assume that would be clear to them.
If you are posting a complete solution to a homework question, at least try to teach something, instead of only providing the opportunity for cheating.

ABHIJIT BISWAS
ABHIJIT BISWAS le 29 Nov 2020
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
% Check that inputs are positive
elseif ~all([y, m, d] > 0)
isvalid = false;
% Check that inputs are integers (not the data type)
elseif any(rem([y, m, d], 1))
isvalid = false;
% Check that m and d are below the max possible
elseif (m > 12) || (d > 31)
isvalid = false;
% The inputs could be a valid date, let's see if they actually are
else
% Vector of the number of days for each month
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb
if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0))
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay
isvalid = false;
else
isvalid = true;
end
end
end

Chandan Kumar
Chandan Kumar le 3 Mar 2021
function valid = valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif m<=12 && (ismember(m, [4 6 9 11]) && ismember(d, [1:30]))
valid=true;
elseif m<=12 && (ismember(m, [1 3 5 7 8 10 12]) && ismember(d, 1:31))
valid=true;
else
valid = false
end
% This is the shortest and oring code for all the valid date i could write
% date formaat should be in lie vald_date(year,month,date) to mae the function work
  1 commentaire
Rik
Rik le 3 Mar 2021
Shorter code is possible with a different strategy, see e.g. this answer.
My opinion would be that the shortest code is not always the best code. The best code in my opinion would be maintainable (so well-commented), as well as fast.

Shun Yan
Shun Yan le 5 Avr 2021
Why would this code not pass the scalar test? %Return false if the input is not scalar
  3 commentaires
Shun Yan
Shun Yan le 5 Avr 2021
oh so tthe isscalar can't be used in tthat whole bunch of &&? So can i just write a seperate if statements with all tthe ~isscalars att the front, I think that'll work right?
Walter Roberson
Walter Roberson le 5 Avr 2021
You need to have the isscalar() check before the other checks.
if ~all(iscalar(a) && isscalar(b) && isscalar(c))
do whatever appropriate for error
end

freddy alexander  rodriguez torres
Modifié(e) : freddy alexander rodriguez torres le 12 Avr 2021
function valid=valid_date(y,m,d)
k=y/4;
j=y/400;
i=y/100;
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y~=fix(y) || m~=fix(m) || d~=fix(d)
valid=false;
elseif (k==fix(k) || j==fix(j)) && m==2 && d<=29 && i~=fix(i) && d>0
valid=true;
elseif j==fix(j) && m==2 && d<=29 && d>0
valid=true;
elseif (ismember(m, [1 3 5 7 8 10 12])) && d<=31 && d>0
valid=true;
elseif (ismember(m, [4 6 9 11])) && d<=30 && d>0
valid=true;
elseif m==2 && d<=28 && i==fix(i) && d>0
valid=true;
elseif m==2 && d<=28 && i~=fix(i) && d>0
valid=true;
else
valid=false;
end

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by