Five weekends: Difference between revisions

(+Stata)
Line 3,801:
 
=={{header|Julia}}==
<lang{{works with|Julia>|0.6}}
isdefined(:Date) || using Dates
 
<lang julia>isweekend(dt::Date) = Dates.dayofweek(dt) ∈ (Dates.Friday, Dates.Saturday, Dates.Sunday)
const wday = Dates.Fri
const lo = 1900
const hi = 2100
const showres = 5
 
function hasfiveweekend(month::Integer, year::Integer)
mfive = recur(Date(lo, 1):Month(1):Date(hi, 12)) do m
dmin = Date(year, month, 1)
Dates.daysinmonth(m) == 31 && Dates.dayofweek(m) == wday
dmax = dmin + Dates.Day(Dates.daysinmonth(dmin) - 1)
return count(isweekend, dmin:dmax) ≥ 15
end
 
months = collect((y, m) for y in 1900:2100, m in 1:12 if hasfiveweekend(m, y))
println("Considering the years from ", lo, " to ", hi, ".\n")
println("There are ", length(mfive), " months having 5 3-day weekends.")
 
println("TheNumber firstof ",months showres,with "5 suchfull-weekends: $(length(months are:))")
println("First years that have nofive such months.:")
for m in mfive[1:showres]
for (y, m) in months[1:5] println(" - $y-$m", Dates.monthname(m), " ", Dates.year(m))end
println("\nTheLast last ", showres, "five such months are:")
end
for (y, m) in months[end-4:end] println(" - $y-$m") end
 
# extra credit
println("\nThe last ", showres, " such months are:")
yrs = getindex.(months, 1)
for m in mfive[end-showres+1:end]
nyrs = 2100 - 1899 - length(unique(yrs))
println(" ", Dates.monthname(m), " ", Dates.year(m))
end
 
println("Number of year with not one 5-full-weekend month: $nyrs")</lang>
print("\nThere are ", length(filter(y -> !(y in year(mfive)), lo:hi)))
println(" years that have no such months.")
</lang>
 
This code uses the <code>Dates</code> module, which is being incorporated into Julian's standard library with the current development version (<tt>0.4</tt>). I've used <code>isdefined</code> to make this code good for the current stable version (<tt>0.3</tt>) as well as for future releases. If <code>Dates</code> is not installed on your instance of Julian try <code>Pkg.add("Dates")</code> from the <tt>REPL</tt>.
 
{{out}}
<pre>Number of months with 5 full-weekends: 201
<pre>
TheFirst last 5five such months are:
Considering the years from 1900 to 2100.
- 1904-1
 
- 1909-1
There are 201 months having 5 3-day weekends.
- 1915-1
The first 5 such months are:
- 1926-1
March 1901
- 1932-1
August 1902
TheLast first 5five such months are:
May 1903
- 2062-12
January 1904
- 2073-12
July 1904
- 2079-12
 
- 2084-12
The last 5 such months are:
- 2090-12
March 2097
Number of year with not one 5-full-weekend month: 29</pre>
August 2098
May 2099
January 2100
October 2100
 
There are 29 years that have no such months.
</pre>
 
=={{header|k}}==
Anonymous user