Five weekends: Difference between revisions

m
→‎{{header|REXX}}: added a version that'll work with any REXX interpreter.
m (→‎{{header|REXX}}: added REXX language.)
m (→‎{{header|REXX}}: added a version that'll work with any REXX interpreter.)
Line 789:
 
=={{header|REXX}}==
===version 1===
This version uses the latest enhancements to the DATE built-in function.
<br>Not all REXX interpreters support this feature.
<br><br>
Programming justification note: the inclusion of leapyear checking is present even though it wasn't needed as
<br>February can be excluded from the criteria of having five weekends. Having the leapyear routine included
<br>allows for a general-purpose strategem of solving these types of problems without taking shortcuts to the
<br>solution (although shortcuts are generaly desireable). If there is a change in the criteria, the
<br>support for more (or less) checks can be supported easily, as well as supporting more criterium.
<lang rexx>
/*REXX program finds months with 5 weekends in them (given a date range)*/
Line 1,086 ⟶ 1,095:
There are 29 years that haven't any five-weekend months in years 1900-->2100
</pre>
===version 2===
This version will work with any version of a REXX interpreter.
<lang rexx>
/*REXX program finds months with 5 weekends in them (given a date range)*/
 
parse arg yStart yStop .
if yStart=='' then yStart=1900
if yStop =='' then yStop =2100
years=ystop-yStart+1
month. =31 /*month days, Feb. is done later.*/
month.4 =30
month.6 =30
month.9 =30
month.11=30
haps=0 /*num of five weekends happenings*/
yr5.=0 /*if a year has any five-weekends*/
@months='January February March April May June July',
'August September October November December'
 
do y=yStart to yStop /*process the years specified. */
do m=1 for 12 /*process each month in each year*/
wd.=0
if m==2 then month.2=28+leapyear(y) /*handle num of days in Feb*/
do d=1 for month.m
_=dow(m,d,y) /*get day-of-week for mm/dd/yyyy.*/
wd._=wd._+1 /*_: 1 (Sunday), 2 (Monday) ...*/
end /*d*/
if wd.1\==5 | wd.6\==5 | wd.7\==5 then iterate /*5 weekends ?*/
haps=haps+1 /*bump counter*/
say 'There are five weekends in' y word(@months,m)
yr5.y=1 /*indicate this year has one. */
end /*m*/
end /*y*/
 
say
say 'There were' haps "occurance"s(haps) 'of five-weekend months in',
"year"s(years) yStart'-->'yStop
say
no5s=0
 
do y=yStart to yStop
if yr5.y then iterate
no5s=no5s+1
say y "doesn't have any five-weekend months."
end
 
say
say "There are" no5s 'year's(no5s),
"that haven't any five-weekend months in year"s(years) yStart'-->'yStop
exit
 
 
/*─────────────────────────────────────DOW (day of week) subroutine─────*/
dow: procedure; arg m,d,y; if m<3 then do; m=m+12; y=y-1; end
yL=left(y,2); yr=right(y,2); w=(d+(m+1)*26%10+yr+yr%4+yL%4+5*yL)//7
if w==0 then w=7; return w /*Sunday=1, Monday=2, ... Saturday=7*/
 
 
/*─────────────────────────────────────LEAPYEAR subroutine──────────────*/
leapyear: procedure; arg y /*year could be: Y, YY, YYY, YYYY*/
if length(y)==2 then y=left(right(date(),4),2)y /*adjust for YY year.*/
if y//4\==0 then return 0 /* not ≈ by 4? Not a leapyear.*/
return y//100\==0 | y//400==0 /*apply 100 and 400 year rule. */
 
 
/*─────────────────────────────────────S subroutine (for plurals)───────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
</lang>
 
=={{header|Ruby}}==