Five weekends: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Picat}}: Added {{out}})
m (syntax highlighting fixup automation)
Line 30:
For maximum compatibility, this program uses only the basic instruction set (S/360)
and two ASSIST macros (XDECO, XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* Five weekends 31/05/2016
FIVEWEEK CSECT
USING FIVEWEEK,R13 base register
Line 129:
XDEC DS CL12 temp for XDECO
YREGS
END FIVEWEEK</langsyntaxhighlight>
{{out}}
<pre>
Line 155:
=={{header|Action!}}==
Day of the week is determined using [https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto.27s_methods Sakamoto] method.
<langsyntaxhighlight Actionlang="action!">;https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto.27s_methods
BYTE FUNC DayOfWeek(INT y BYTE m,d) ;1<=m<=12, y>1752
BYTE ARRAY t=[0 3 2 5 0 3 5 1 4 6 2 4]
Line 204:
PrintI(years(i)) Put('/) PrintBE(months(i))
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Five_weekends.png Screenshot from Atari 8-bit computer]
Line 225:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar.Formatting; use Ada.Calendar;
Line 249:
Put_Line ("Number of months:" & Integer'Image (Months));
end Five_Weekends;
</syntaxhighlight>
</lang>
{{out|Sample output}}
<pre style="height:30ex;overflow:scroll">
Line 461:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">five_weekends: BEGIN
INT m, year, nfives := 0, not5 := 0;
BOOL no5weekend;
Line 513:
END # function day_of_week #;
SKIP
END # program five_weekends #</langsyntaxhighlight>
{{out}}
<pre>
Line 535:
===Imperative===
 
<langsyntaxhighlight AppleScriptlang="applescript">set fiveWeekendMonths to {}
set noFiveWeekendYears to {}
 
Line 582:
set y to y + 12
end
resultText</langsyntaxhighlight>
 
{{out}} (can always add "display dialog resultText" for GUI output):
Line 595:
 
===Functional===
<langsyntaxhighlight AppleScriptlang="applescript">-- TEST -----------------------------------------------------------------------
on run
Line 767:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{{|number|:201},
{firstFive:{"1901 March", "1902 August", "1903 May", "1904 January", "1904 July"}},
{lastFive:{"2097 March", "2098 August", "2099 May", "2100 January", "2100 October"}},
Line 775:
{leanYears:{1900, 1906, 1917, 1923, 1928, 1934, 1945, 1951, 1956, 1962, 1973, 1979,
1984, 1990, 2001, 2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074,
2085, 2091, 2096}}}</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Year := 1900
End_Year := 2100
31_Day_Months = 01,03,05,07,08,10,12
Line 814:
Years with no 5 day weekends between 1900 and 2100 : %No_5_Weekend_Count%
%All_Years_Without_5_Weekend%
)</langsyntaxhighlight>
 
=={{header|AutoIt}}==
Line 821:
2 - Years Without 5 Weekend Months
3 - Year and Month with 5 Weekends
<syntaxhighlight lang="autoit">
<lang AutoIt>
#include <Date.au3>
#include <Array.au3>
Line 863:
EndIf
EndFunc ;==>Five_weekends
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
 
<langsyntaxhighlight lang="awk">
# usage: awk -f 5weekends.awk cal.txt
 
Line 924:
print(bY)
}
</syntaxhighlight>
</lang>
 
See also: [http://rosettacode.org/wiki/Five_weekends#UNIX_Shell unix-shell] and [http://rosettacode.org/wiki/Calendar#AWK Calendar].
Line 953:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"DATELIB"
DIM Month$(12)
Month$() = "","January","February","March","April","May","June", \
Line 970:
IF num% = oldnum% PRINT "(none)" ELSE PRINT
NEXT year%
PRINT "Total = " ; num%</langsyntaxhighlight>
{{out}}
<pre>
Line 993:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <time.h>
 
Line 1,022:
printf("%d total\n", n);
return 0;
}</langsyntaxhighlight>
{{out}} (note that the C library may not be able to handle all dates; the output may vary across systems):
<pre>
Line 1,054:
 
Not your typical method. Requires <code>ncal</code>.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 1,093:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,106:
=={{header|C sharp|C#}}==
===With iteration===
<langsyntaxhighlight lang="csharp">using System;
 
namespace _5_Weekends
Line 1,142:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,163:
 
===With LINQ===
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 1,199:
{
public static IEnumerable<int> To(this int start, int end) => Enumerable.Range(start, end - start + 1);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,222:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <vector>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <algorithm>
Line 1,264:
std::cout << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre style="height:30ex;overflow:scroll">
Line 1,505:
=={{header|Ceylon}}==
<b>module.ceylon</b>
<langsyntaxhighlight lang="ceylon">
module rosetta.fiveweekends "1.0.0" {
import ceylon.time "1.2.2";
}
</syntaxhighlight>
</lang>
<b>run.ceylon</b>
<langsyntaxhighlight lang="ceylon">
import ceylon.time {
date,
Line 1,568:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,606:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(import java.util.GregorianCalendar
java.text.DateFormatSymbols)
 
Line 1,616:
(println month "-" year))
count
(println "Total Months: " ,))</langsyntaxhighlight>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
<lang COBOL>
program-id. five-we.
data division.
Line 1,673:
.
end program five-we.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,692:
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
startsOnFriday = (month, year) ->
# 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
Line 1,734:
fiveWeekends()
 
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="text">
1901 2
1902 7
Line 1,767:
 
Years with no five-weekend months: 29
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;; Given a date, get the day of the week. Adapted from
;; http://lispcookbook.github.io/cl-cookbook/dates_and_times.html
 
Line 1,816:
(format t "Last 5 months: ~A~%" (subseq good-months 0 5))
(format t "Years without a five-weekend month: ~A~%" bad-years))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,825:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.datetime, std.algorithm, std.range;
 
Date[] m5w(in Date start, in Date end) pure /*nothrow*/ {
Line 1,855:
writefln("\nThere are %d years in the range that do not have " ~
"months with five weekends.", n);
}</langsyntaxhighlight>
{{out}}
<pre>There are 201 months of which the first and last five are:
Line 1,871:
There are 29 years in the range that do not have months with five weekends.</pre>
===Simpler Version===
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.datetime, std.traits;
 
Line 1,905:
writeln("\nTotal number of years with no 5-weekend months: ",
totalNo5Weekends);
}</langsyntaxhighlight>
{{out}}
<pre>Total 5-weekend months between 1900 and 2100: 201
Line 1,924:
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">
<lang Dart>
main() {
var total = 0;
Line 1,938:
print('Year with none: $empty');
}
</syntaxhighlight>
</lang>
{{out|Sample output}}
<pre style="height:30ex;overflow:scroll">
Line 1,955:
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">program FiveWeekends;
 
{$APPTYPE CONSOLE}
Line 1,988:
Writeln(Format('Months with 5 weekends: %d', [lFiveWeekendCount]));
Writeln(Format('Years with no 5 weekend months: %d', [lYearsWithout]));
end.</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Date do
@months { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" }
Line 2,008:
IO.puts "Found #{count} month with 5 weekends."
IO.puts "\nFound #{length(none)} years with no month having 5 weekends:"
IO.puts "#{inspect Enum.map(none, fn {y,_}-> y end)}"</langsyntaxhighlight>
 
{{out}}
Line 2,032:
Two examples, both based on first building a nested list-of-lists like [Year1, Year2, ..., YearN], where each year is a sublist of year and month tuples, like [{YearN, 1}, {YearN, 2}, ..., {YearN, 12}].<br>
First, a pretty compact example intended for use with <b>escript</b>:
<langsyntaxhighlight lang="erlang">
#!/usr/bin/env escript
%%
Line 2,062:
has_five({_Year, _Month}, _DaysNot31) ->
false.
</syntaxhighlight>
</lang>
Second, a more verbose Erlang module:
<langsyntaxhighlight lang="erlang">
-module(five_weekends).
 
Line 2,116:
find_years_without_5w_months(List) ->
[Y || {[{Y,_M}|_], 0} <- List].
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,164:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM FIVE_WEEKENDS
 
Line 2,201:
PRINT("Total =";CNT%)
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,223:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
--Five Weekend task from Rosetta Code wiki
--User:Lnettnay
Line 2,266:
puts(1, "Number of years that have no months with five full weekends = ")
? none
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,285:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
[<EntryPoint>]
Line 2,318:
(List.take 5 allMonthsWith5we)
(List.take 5 (List.skip ((List.length allMonthsWith5we) - 5) allMonthsWith5we))
0</langsyntaxhighlight>
{{out}}
<pre>201 months in the range of years from 1900 to 2100 have 5 weekends.
Line 2,325:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: calendar calendar.format formatting io kernel math
sequences ;
 
Line 2,340:
[ length "%d five-weekend months found.\n" printf ]
[ 5 head timestamps>my "..." print ]
[ 5 tail* timestamps>my ] tri</langsyntaxhighlight>
{{out}}
<pre>
Line 2,361:
 
Using Zeller's congruence
<langsyntaxhighlight lang="fortran">program Five_weekends
implicit none
 
Line 2,417:
end function Day_of_week
end program Five_weekends</langsyntaxhighlight>
{{out}}
<pre>Mar 1901
Line 2,434:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 23-06-2015
' compile with: fbc -s console
 
Line 2,494:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>1901 | March
Line 2,518:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=fbeb1b7a06fff3da3f8b262ee5d71390 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim aMonth As Short[] = [1, 3, 5, 7, 8, 10, 12] 'All 31 day months
Dim aMMStore As New String[] 'To store results
Line 2,555:
" years that do not have at least one five-weekend month" 'Print the amount of years with no 5 weekend months
Print aMMStore.Join(",") 'Print the years with no 5 weekend months
End</langsyntaxhighlight>
Output:
<pre>
Line 2,577:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># return a list of two lists :
# first is the list of months with five weekends between years y1 and y2 (included)
# second is the list of years without such months, in the same interval
Line 2,609:
# [ "Mar-1901", "Aug-1902", "May-1903", "Jan-1904", "Jul-1904" ]
r[1]{[n-4 .. n]};
# [ "Mar-2097", "Aug-2098", "May-2099", "Jan-2100", "Oct-2100" ]</langsyntaxhighlight>
 
=={{header|Go}}==
Using second algorithm suggestion:
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,662:
}
fmt.Println(len(haveNone), "total")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,709:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">enum Day {
Sun, Mon, Tue, Wed, Thu, Fri, Sat
static Day valueOf(Date d) { Day.valueOf(d.format('EEE')) }
Line 2,725:
}
}.flatten()
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">def ym = { it.format('yyyy-MM') }
def years = 1900..2100
def fiveWeekendMonths = fiveWeekends(years)
println "Number of five weekend months: ${fiveWeekendMonths.size()}"
fiveWeekendMonths.each { println (ym(it)) }</langsyntaxhighlight>
 
{{out}}
Line 2,941:
 
Test:
<langsyntaxhighlight lang="groovy">def yearsWith = fiveWeekendMonths.collect { it.format('yyyy') as int } as Set
def yearsWithout = (years as Set) - yearsWith
println "\nNumber of years without a five weekend month: ${yearsWithout.size()}"
yearsWithout.each { println it }</langsyntaxhighlight>
 
{{out}}
Line 2,979:
 
=={{header|Harbour}}==
<langsyntaxhighlight lang="visualfoxpro">
PROCEDURE Main()
LOCAL y, m, d, nFound, cNames, nTot := 0, nNotFives := 0
Line 3,013:
? "Years with no five weekends months: " + hb_NtoS( nNotFives )
 
RETURN</langsyntaxhighlight>
 
{{out}}
Line 3,048:
 
Now it is easy to get all months with 31 days that start on Friday.
<langsyntaxhighlight Haskelllang="haskell">import Data.List (intercalate)
 
data DayOfWeek = Monday | Tuesday | Wednesday | Thursday | Friday |
Line 3,151:
formatMonth :: (Year, [Month]) -> String
formatMonth (y, m) = show y ++ ": " ++ intercalate ", " [ show x | x <- m ]</langsyntaxhighlight>
{{out}}
<pre>
Line 3,177:
 
====Using Data.Time====
<langsyntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.List.Split (chunksOf)
import Data.Time (Day, fromGregorian, gregorianMonthLength)
Line 3,223:
('\t' :) . unwords . fmap show
<$> chunksOf 5 lean
]</langsyntaxhighlight>
{{Out}}
<pre>How many five-weekend months 1900-2100 ?
Line 3,251:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link datetime,printf
 
procedure main(A) # five weekends
Line 3,276:
every insert(F := set(), integer(!fwe ? tab(find("-"))))
return sort(Y--F)
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 3,329:
Inform 7 has no built-in date functions, so this solution implements date types and a day-of-week function.
 
<langsyntaxhighlight lang="inform7">Calendar is a room.
 
When play begins:
Line 3,430:
repeat with M running through months:
if M of Y is a happy month, decide no;
decide yes.</langsyntaxhighlight>
 
{{out}}
Line 3,447:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">require 'types/datetime numeric'
find5wkdMonths=: verb define
years=. range 2{. y
Line 3,453:
m5w=. (#~ 0 = weekday) >,{years;months;31 NB. 5 full weekends iff 31st is Sunday(0)
>'MMM YYYY' fmtDate toDayNo m5w
)</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight lang="j"> # find5wkdMonths 1900 2100 NB. number of months found
201
(5&{. , '...' , _5&{.) find5wkdMonths 1900 2100 NB. First and last 5 months found
Line 3,470:
Oct 2100
# (range -. {:"1@(_ ". find5wkdMonths)) 1900 2100 NB. number of years without 5 weekend months
29</langsyntaxhighlight>
 
=={{header|Java}}==
In Java 1.5+ you can add <code>import static java.util.Calendar.*;</code> to the list of imports and remove all other occurrences of <code>Calendar.</code> from the rest of the code (e.g. <code>Calendar.FRIDAY</code> would simply become <code>FRIDAY</code>). It's more portable (and probably more clear) to leave the <code>Calendar.</code>'s in.
<langsyntaxhighlight lang="java">import java.util.Calendar;
import java.util.GregorianCalendar;
 
Line 3,505:
}
}
}</langsyntaxhighlight>
{{out}} (middle results cut out):
<pre style="height:30ex;overflow:scroll"> There are 201 months with five weekends from 1900 through 2100:
Line 3,566:
===ES5===
====Imperative====
<langsyntaxhighlight lang="javascript">function startsOnFriday(month, year)
{
// 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
Line 3,610:
document.write('Years with no five-weekend months: ' + yearsWithoutFiveWeekends.length + '<br>');
}
fiveWeekends();</langsyntaxhighlight>
 
{{out|Sample output}}
Line 3,631:
 
Here is an alternative solution that uses the offset between the first day of every month, generating the same solution but without relying on the Date object.
<langsyntaxhighlight lang="javascript">var Months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
Line 3,691:
 
console.log('Number of months that have five full weekends from 1900 to 2100: ' + total);
console.log('Number of years without any five full weekend months: ' + without);</langsyntaxhighlight>
{{out}}
<pre>1901-Mar
Line 3,708:
 
====Functional====
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
'use strict';
 
Line 3,761:
null, 2
);
})();</langsyntaxhighlight>
{{Out}}
<pre>{
Line 3,783:
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
// longMonthsStartingFriday :: Int -> [Int]
const longMonthsStartingFriday = y =>
Line 3,842:
leanYearCount: lstLeanYears.length
});
})();</langsyntaxhighlight>
{{Out}}
<pre>{
Line 3,867:
 
'''Foundations: Zeller's Congruence'''
<langsyntaxhighlight lang="jq"> Use Zeller's Congruence to determine the day of the week, given
# year, month and day as integers in the conventional way.
# Emit 0 for Saturday, 1 for Sunday, etc.
Line 3,881:
+ (.[1]/400|floor) - 2*(.[1]/100|floor)
| . % 7
;</langsyntaxhighlight>
'''Nuts and Bolts''':
<langsyntaxhighlight lang="jq">def weekday_of_last_day_of_month(year; month):
def day_before(day): (6+day) % 7;
 
Line 3,907:
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][.-1];
"\(.[1] | month) \(.[0])"
;</langsyntaxhighlight>
'''The Task''':
<langsyntaxhighlight lang="jq">five_weekends(1900;2101)
| "There are \(length) months with 5 weekends from 1900 to 2100 inclusive;",
"the first and last five are as follows:",
Line 3,915:
"...",
( .[length-5: ][] | pp),
"In this period, there are \( [range(1900;2101)] - map( .[0] ) | length ) years which have no five-weekend months."</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -r -n -f Five_Weekends.jq
There are 201 months with 5 weekends from 1900 to 2100 inclusive;
the first and last five are as follows:
Line 3,931:
Jan 2100
Oct 2100
In this period, there are 29 years which have no five-weekend months.</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">isweekend(dt::Date) = Dates.dayofweek(dt) ∈ (Dates.Friday, Dates.Saturday, Dates.Sunday)
 
function hasfiveweekend(month::Integer, year::Integer)
Line 3,956:
nyrs = 2100 - 1899 - length(unique(yrs))
 
println("Number of year with not one 5-full-weekend month: $nyrs")</langsyntaxhighlight>
 
{{out}}
Line 3,975:
 
=={{header|k}}==
<syntaxhighlight lang="k">
<lang k>
cal_j:(_jd[19000101]+!(-/_jd 21010101 19000101)) / enumerate the calendar
is_we:(cal_j!7) _lin 4 5 6 / identify friday saturdays and sundays
Line 3,989:
yn5:y@&~y _lin y5 / find any years not in the 5 weekend month list
`0:,"There are ",($#yn5)," years without any five-weekend months"
`0:,1_,/",",/:$yn5</langsyntaxhighlight>
{{out}}
<pre>
Line 4,008:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.util.*
Line 4,034:
println("There are ${yearsWithNone.size} years with no months which have 5 weekends, namely:")
println(yearsWithNone)
}</langsyntaxhighlight>
 
{{out}}
Line 4,047:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(
months = array(1, 3, 5, 7, 8, 10, 12),
fivemonths = array,
Line 4,098:
}
 
#output</langsyntaxhighlight>
{{out|Result}}
<pre>Total number of months 201
Line 4,107:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
local months={"JAN","MAR","MAY","JUL","AUG","OCT","DEC"}
local daysPerMonth={31+28,31+30,31+30,31,31+30,31+30,0}
Line 4,138:
print("Months with 5 weekends: ",cnt_months)
print("Years without 5 weekends in the same month:",cnt_no5we)
</syntaxhighlight>
</lang>
{{out}}
<pre>1900 0:
Line 4,168:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">five_weekends:= proc()
local i, month, count;
#Only months with 31 days can possibly satisfy the condition
Line 4,184:
printf("%d months have five full weekends.\n", count);
end proc;
five_weekends();</langsyntaxhighlight>
{{Out|Output}}
<pre>1901-March
Line 4,390:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">years = {1900, 2100}; months = {1 ,3 ,5 ,7 ,8 ,10 ,12};
result = Select[Tuples[{Range@@years, months}], (DateString[# ~ Join ~ 1, "DayNameShort"] == "Fri")&];
 
Line 4,397:
Print["Last months: " , DateString[#,{"MonthName"," ","Year"}]& /@ result[[-5 ;; All]]];
Print[# // Length, " years without 5 weekend months:\n", #] &@
Complement[Range @@ years, Part[Transpose@result, 1]];</langsyntaxhighlight>
{{out}}
<pre>201 months with 5 weekends
Line 4,409:
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight Matlablang="matlab">longmonth = [1 3 5 7 8 10 12];
 
i = 1;
Line 4,433:
for j = length(x)-4:length(x)
fprintf('\t %s \n',x(j,:))
end</langsyntaxhighlight>
{{out}}
<pre>There are 201 months with 5 weekends between 1900 and 2100.
Line 4,452:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">left(a, n) := makelist(a[i], i, 1, n)$
right(a, n) := block([m: length(a)], makelist(a[i], i, m - n + 1, m))$
 
Line 4,468:
 
right(a, 5);
[[2097,3],[2098,8],[2099,5],[2100,1],[2100,10]]</langsyntaxhighlight>
 
=={{header|MUMPS}}==
{{libheader|VA Kernel|22.0}}
<syntaxhighlight lang="mumps">
<lang MUMPS>
FIVE
;List and count the months between 1/1900 and 12/2100 that have 5 full weekends
Line 4,509:
. S:'N L=L_$S($L(L):",",1:"")_(R+1700)
W !,"For a total of "_C_" months.",!!,"There are "_$L(L,",")_" years with no five full weekends in any month.",!,"They are: "_L
Q</langsyntaxhighlight>Usage:<pre>
USER>d ^FIVE
 
Line 4,535:
=={{header|NetRexx}}==
 
<langsyntaxhighlight lang="netrexx">
/* NetRexx ************************************************************
* 30.08.2012 Walter Pachl derived from Rexx version 3
Line 4,581:
yya=yy+4800+mma
result=d-32075+1461*yya%4+367*(mm-2-mma*12)%12-3*((yya+100)%100)%4
Return result /* return the result */</langsyntaxhighlight>
Output: see Rexx version 3
 
=={{header|NewLISP}}==
<langsyntaxhighlight lang="newlisp">
#!/usr/local/bin/newlisp
 
Line 4,663:
(println "Years with no five weekends months: " NotFives)
(exit)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,683:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import times
 
const LongMonths = {mJan, mMar, mMay, mJul, mAug, mOct, mDec}
Line 4,696:
if none: inc sumNone
 
echo "\nYears without a 5 weekend month: ", sumNone</langsyntaxhighlight>
{{out}}
<pre>March 1901
Line 4,716:
{{libheader|OCaml Calendar Library}}
 
<langsyntaxhighlight lang="ocaml">open CalendarLib
 
let list_first_five = function
Line 4,742:
List.iter print_month (list_first_five (List.rev !months));
List.iter print_month (List.rev (list_first_five !months));
;;</langsyntaxhighlight>
 
{{out}}
Line 4,761:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: fiveWeekEnd(y1, y2)
| y m |
 
Line 4,772:
]
]
dup size println dup left(5) println right(5) println ;</langsyntaxhighlight>
 
{{out}}
Line 4,783:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">fiveWeekends()={
my(day=6); \\ 0 = Friday; this represents Thursday for March 1, 1900.
my(ny=[31,30,31,30,31,31,30,31,30,31,31,28],ly=ny,v,s);
Line 4,802:
);
s
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 4,808:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
use DateTime ;
 
Line 4,841:
}
print "No long weekends in the following " . @workhardyears . " years:\n" ;
map { print "$_\n" } @workhardyears ;</langsyntaxhighlight>
{{out}}
<pre>There are 201 months with 5 full weekends!
Line 4,888:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">m31</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"January"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"March"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"May"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"July"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"August"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"October"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"December"</span><span style="color: #0000FF;">}</span>
Line 4,920:
<span style="color: #000000;">none</span><span style="color: #0000FF;">[</span><span style="color: #000000;">6</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">".."</span><span style="color: #0000FF;">}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">none</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,940:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go ?=>
println("Months with five weekends:"),
FiveWeekends = [ [Year,Month] : Year in 1900..2100, Month in [1,3,5,7,8,10,12], dow(Year,Month,1) == 5],
Line 4,986:
(Year mod 4 == 0, Year mod 100 != 0)
;
Year mod 400 == 0. </langsyntaxhighlight>
 
{{out}}
Line 5,002:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq Lst
(make
(for Y (range 1900 2100)
Line 5,018:
(setq Lst (diff (range 1900 2100) (uniq (mapcar cadr Lst))))
(prinl "There are " (length Lst) " years with no five-weekend months:")
(println Lst)</langsyntaxhighlight>
{{out}}
<pre>There are 201 months with five weekends:
Line 5,038:
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">int(0..1) weekends(object day)
{
return (<5,6,7>)[day->week_day()];
Line 5,055:
 
array rest = range->years() - have5->year();
write("%d years without any 5 weekend month:\n %{%d,%}\n", sizeof(rest), rest->year_no());</langsyntaxhighlight>
 
{{out}}
Line 5,077:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
weekends: procedure options (main); /* 28/11/2011 */
declare tally fixed initial (0);
Line 5,103:
 
end weekends;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,185:
=={{header|PowerShell}}==
 
<langsyntaxhighlight lang="powershell">$fiveWeekends = @()
$yearsWithout = @()
foreach ($y in 1900..2100) {
Line 5,204:
Write-Output ""
Write-Output "Extra Credit: these $($yearsWithout.count) years have no such month:"
Write-Output ($yearsWithout -join ",")</langsyntaxhighlight>
 
{{Output}}
Line 5,216:
=={{header|Prolog}}==
Works with SWI-Prolog;
<langsyntaxhighlight Prologlang="prolog">main() :-
weekends(1900, 2100, FiveWeekendList, RemainderWeekendList),
 
Line 5,288:
length(LastN, N),
append(_, LastN, List).
</syntaxhighlight>
</lang>
{{out}}
<pre>?- main.
Line 5,299:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure DateG(year.w, month.b, day)
;Returns the number of days before or after the earliest reference date
;in PureBasic's Date Library (1 Jan 1970) based on an assumed Gregorian calendar calculation
Line 5,364:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out|Sample output}}
<pre>1901 3
Line 5,384:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from datetime import timedelta, date
 
DAY = timedelta(days=1)
Line 5,417:
 
print('\nThere are %i years in the range that do not have months with five weekends'
% len(set(range(START.year, STOP.year)) - {d.year for d in dates}))</langsyntaxhighlight>
 
'''Alternate Algorithm'''
 
The condition is equivalent to having a thirty-one day month in which the last day of the month is a Sunday.
<langsyntaxhighlight lang="python">LONGMONTHS = (1, 3, 5, 7, 8, 10, 12) # Jan Mar May Jul Aug Oct Dec
def fiveweekendspermonth2(start=START, stop=STOP):
return [date(yr, month, 31)
Line 5,431:
 
dates2 = fiveweekendspermonth2()
assert dates2 == dates</langsyntaxhighlight>
 
{{out|Sample output}}
Line 5,451:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ over 3 < if [ 1 - ]
dup 4 / over +
over 100 / -
Line 5,515:
i if say ", "
i^ 8 mod 7 = if
[ cr space 29 of echo$ ] ]</langsyntaxhighlight>
 
{{out}}
Line 5,531:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">ms = as.Date(sapply(c(1, 3, 5, 7, 8, 10, 12),
function(month) paste(1900:2100, month, 1, sep = "-")))
ms = format(sort(ms[weekdays(ms) == "Friday"]), "%b %Y")
message("There are ", length(ms), " months with five weekends.")
message("The first five: ", paste(ms[1:5], collapse = ", "))
message("The last five: ", paste(tail(ms, 5), collapse = ", "))</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require srfi/19)
Line 5,566:
(for ([w (drop weekends (- count 5))])
(displayln (date->string w "~b ~Y")))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,587:
(formerly Perl 6)
{{works with|Rakudo|2015-09-10}}
<syntaxhighlight lang="raku" perl6line># A month has 5 weekends iff it has 31 days and starts on Friday.
 
my @years = 1900 .. 2100;
Line 5,596:
say 'First happy months: ' ~ @happy[^5];
say 'Last happy months: ' ~ @happy[*-5 .. *];
say 'Dreary years count: ', @years - @happy».year.squish;</langsyntaxhighlight>
 
{{out}}
Line 5,611:
<br>Changing the task's requirements to find how many extended weekends (Fr-Sa-Su ''or'' Sa-Su-Mo) there are for
<br>each month would entail changing only a single &nbsp; '''if''' &nbsp; statement.
<langsyntaxhighlight lang="rexx">/*REXX program finds months that contain five weekends (given a date range). */
month. =31; month.2=0 /*month days; February is skipped. */
month.4=30; month.6=30; month.9=30; month.11=30 /*all the months with thirty-days. */
Line 5,641:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*pluralizer.*/</langsyntaxhighlight>
'''output''' &nbsp; when using the default inputs:
<pre style="height:98ex">
Line 5,883:
===version for older REXXes===
This version will work with any version of a REXX interpreter.
<langsyntaxhighlight lang="rexx">/*REXX program finds months that contain five weekends (given a date range). */
month. =31; month,2=0 /*month days; February is skipped. */
month.4=30; month.6=30; month.9=30; month.11=30 /*all the months with thirty-days. */
Line 5,919:
if w==0 then w=7; return w /*Sunday=1, Monday=2, ... Saturday=7 */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*pluralizer.*/</langsyntaxhighlight>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.
<br><br>
 
===version short and focussed at the task description===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* Short(er) solution focussed at the task's description
* Only 7 months can have 5 full weekends
Line 5,978:
Return result /* return the result */
 
leapyear: Return ( (arg(1)//4=0) & (arg(1)//100<>0) ) | (arg(1)//400=0)</langsyntaxhighlight>
{{Out}}
<pre>
Line 6,000:
 
===shorter version===
<langsyntaxhighlight lang="rexx">/*REXX program finds months that contain five weekends (given a date range). */
month. =31; month.2=0 /*month days; February is skipped. */
month.4=30; month.6=30; month.9=30; month.11=30 /*all the months with thirty-days. */
Line 6,029:
say
say "There are " # ' year's(#) "that haven't any" @5w 'in year's(years) yStart'──►'yStop
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.
 
===shorter and more focused===
This REXX version takes advantage that a month with five full weekends must start on a Friday and have 31 days.
<langsyntaxhighlight lang="rexx">/*REXX program finds months that contain five weekends (given a date range). */
month. =31 /*days in "all" the months. */
month.2=0; month.4=0; month.6=0; month.9=0; month.11=0 /*not 31 day months.*/
Line 6,060:
end /*y*/
say
say "There are " # ' year's(#) "that haven't any" @5w 'in year's(years) yStart'──►'yStop</langsyntaxhighlight>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.
<br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
sum = 0
month = list(12)
Line 6,084:
next
see "Total : " + sum + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 6,109:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'date'
# The only case where the month has 5 weekends is when the last day
# of the month falls on a Sunday and the month has 31 days.
Line 6,123:
puts dates.last(5).map {|d| d.strftime("%b %Y") }
puts "There are #{years_4w.size} years without months with 5 weekends:"
puts years_4w.join(", ")</langsyntaxhighlight>
 
'''Output'''
Line 6,144:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">preYear = 1900
for yyyy = 1900 to 2100
for mm = 1 to 12 ' go thru all 12 months
Line 6,169:
end if
preCount = count
next yyyy</langsyntaxhighlight>
 
{{Out}}
Line 6,191:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">extern crate chrono;
use chrono::prelude::*;
 
Line 6,235:
let out = five_weekends(1900, 2100);
assert_eq!(out.len(), 201);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.util.Calendar._
import java.util.GregorianCalendar
 
Line 6,272:
(1900 to 2100) - months.map(_.year) shouldBe expectedNon5erYears
}
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "time.s7i";
 
Line 6,292:
end for;
writeln("Number of months:" <& months);
end func;</langsyntaxhighlight>
 
{{Out}}
Line 6,314:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('DateTime');
 
var happymonths = [];
Line 6,347:
say happymonths.last(5).join("\n");
say "No long weekends in the following #{workhardyears.len} years:";
say workhardyears.join(",");</langsyntaxhighlight>
{{out}}
<pre>
Line 6,368:
=={{header|Simula}}==
{{trans|FreeBasic}}
<langsyntaxhighlight lang="simula">! TRANSLATION OF FREEBASIC VERSION ;
BEGIN
 
Line 6,457:
 
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 6,483:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">clear
set obs `=tm(2101m1)-tm(1900m1)'
gen month=tm(1900m1)+_n-1
Line 6,512:
| 2100m1 |
| 2100m10 |
+---------+</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
set months {}
Line 6,536:
puts [join [list {*}[lrange $months 0 4] ... {*}[lrange $months end-4 end]] \n]
puts "There are [llength $years] years without any five-weekend months"
puts [join $years ","]</langsyntaxhighlight>
{{Out}}
<pre>
Line 6,556:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
LOOP year=1900,2100
Line 6,564:
ENDLOOP
ENDLOOP
</syntaxhighlight>
</lang>
{{Out}}
<pre style='height:30ex;overflow:scroll'>
Line 6,772:
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="text">' ------=< MAIN >=------
' only months with 31 day can have five weekends
' these months are: January, March, May, July, August, October, December
Line 6,839:
100 Print "October"; : Return
110 Print "November"; : Return
120 Print "December"; : Return</langsyntaxhighlight>
{{out}}
<pre>1901 | March
Line 6,868:
* feed this file to an awk-script to look for the months with 5 weekends.
 
<langsyntaxhighlight lang="bash">echo "Creating cal-file..."
echo > cal.txt
for ((y=1900; y <= 2100; y++)); do
Line 6,880:
echo "Looking for month with 5 weekends:"
awk -f 5weekends.awk cal.txt
</syntaxhighlight>
</lang>
 
See also: [http://rosettacode.org/wiki/Five_weekends#AWK awk] and [[Calendar]]. <br>
Line 6,886:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 6,908:
Debug.Print "There is " & cptm & " months with five full weekends from the year 1900 through 2100"
Debug.Print "There is " & cpt & " years which don't have months with five weekends"
End Sub</langsyntaxhighlight>
{{out}}
<pre>1901, 3
Line 6,934:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight lang="vbscript">
<lang VBScript>
For y = 1900 To 2100
For m = 1 To 12
Line 6,946:
 
WScript.Echo vbCrLf & "Total = " & i & " months"
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{libheader|Wren-date}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/date" for Date
import "/seq" for Lst
 
Line 6,991:
System.print("\n There are %(years.count) years that do not have at least one five-weekend month, namely:")
var chunks = Lst.chunks(years, 10)
for (i in 0...chunks.count) System.print(" %(chunks[i])")</langsyntaxhighlight>
 
{{out}}
Line 7,017:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
 
Line 7,054:
];
CrLf(0); IntOut(0, C); CrLf(0); \show number of years
]</langsyntaxhighlight>
 
{{Out}}
Line 7,077:
=={{header|zkl}}==
Months with five weekends:
<langsyntaxhighlight lang="zkl">var [const] D=Time.Date, r=L();
foreach y,m in ([1900..2100],[1..12]){
if (D.daysInMonth(y,m)==31 and 5==D.weekDay(y,m,1)) r.append(String(y,"-",m))
}</langsyntaxhighlight>
Alternatively, in a functional style using list comprehensions:
<langsyntaxhighlight lang="zkl">r:=[[(y,m); [1900..2100];
[1..12],{D.daysInMonth(y,m)==31 and 5==D.weekDay(y,m,1)};
{String(y,"-",m)}]];</langsyntaxhighlight>
{{out}}
<pre>
Line 7,093:
 
Extra credit: Years with no five day weekends:
<langsyntaxhighlight lang="zkl">var [const] D=Time.Date, r=L();
foreach y in ([1900..2100]){ yes:=True;
foreach m in ([1..12]){
Line 7,099:
}
if (yes) r.append(y)
}</langsyntaxhighlight>
Alternatively:
<langsyntaxhighlight lang="zkl">var yes, r=[[(y,m);
[1900..2100];
[1..12],{if(m==1)yes=True; if(D.daysInMonth(y,m)==31 and 5==D.weekDay(y,m,1)) yes=False; True},
{if (m==12) yes else False};
{y}]]</langsyntaxhighlight>
Bit of a sleaze using a global var (yes) to hold state.
First filter: On the first month, reset global, note if month has five weekends, always pass.
10,333

edits