Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -12,3 +12,8 @@ The month of October in 2010 has five Fridays, five Saturdays, and five Sundays.
'''Extra credit'''
Count and/or show all of the years which do not have at least one five-weekend month (there should be 29).
;See also
* [[Day of the week]]
* [[Last Friday of each month]]
* [[Find last sunday of each month]]

View file

@ -0,0 +1,5 @@
---
category:
- Date and time
- Puzzles
note: Five weekends

View file

@ -0,0 +1,56 @@
# usage: awk -f 5weekends.awk cal.txt
# Filter a file of month-calendars, such as
# ...
## January 1901
## Mo Tu We Th Fr Sa Su
## 1 2 3 4 5 6
## 7 8 9 10 11 12 13
## 14 15 16 17 18 19 20
## 21 22 23 24 25 26 27
## 28 29 30 31
# ...
## March 1901
## Mo Tu We Th Fr Sa Su
## 1 2 3
## 4 5 6 7 8 9 10
## 11 12 13 14 15 16 17
## 18 19 20 21 22 23 24
## 25 26 27 28 29 30 31
# ...
# This file is generated by a script for the unix-shell,
# see http://rosettacode.org/wiki/Five_weekends#UNIX_Shell
BEGIN { print("# Month with 5 weekends:")
badYears = numW5 = 0;
lastW5 = -1
}
0+$2>33 { if( $2>currYear ) { # calendar-header: month, year
if( lastW5==numW5 ) {
badYears++; sep="\n"
if( badYears % 10 ) { sep=" " }
bY=bY currYear sep; # collect years in string
##print badYears,":", currYear
}
lastW5=numW5
}
WE=0; currYear=$2; currMY = $1 " " $2;
##print currMY;
next
}
/^Mo/ { next } # skip lines with weekday-names
{ $0 = substr($0,13) } # cut inputline, leave Fr,Sa,Su only
NF>2 { WE++; # 3 fields left => complete weekend found
if( WE>4 ) {
numW5++; printf("%4d : %s\n", numW5, currMY)
}
}
END { print("# Found", numW5, "month with 5 weekends.")
print("# Found", badYears, "years with no month having 5 weekends:")
print(bY)
}

View file

@ -1,16 +1,16 @@
import std.stdio, std.datetime, std.traits;
void main() {
import std.stdio, std.datetime, std.traits;
enum first_year = 1900;
enum last_year = 2100;
int totalNo5Weekends;
const(Date)[] fiveWeekendMonths;
foreach (year; first_year .. last_year + 1) {
uint totalNo5Weekends;
immutable(Date)[] fiveWeekendMonths;
foreach (immutable year; first_year .. last_year + 1) {
bool has5Weekends = false;
foreach (month; [EnumMembers!Month]) {
const firstDay = Date(year, month, 1);
foreach (immutable month; EnumMembers!Month) {
immutable firstDay = Date(year, month, 1);
if (firstDay.daysInMonth == 31 &&
firstDay.dayOfWeek == DayOfWeek.fri) {
has5Weekends = true;
@ -24,11 +24,11 @@ void main() {
writefln("Total 5-weekend months between %d and %d: %d",
first_year, last_year, fiveWeekendMonths.length);
foreach (date; fiveWeekendMonths[0 .. 5])
writeln(date.month, " ", date.year);
writeln("...");
foreach (date; fiveWeekendMonths[$ - 5 .. $])
writeln(date.month, " ", date.year);
foreach (immutable date; fiveWeekendMonths[0 .. 5])
writeln(date.month, ' ', date.year);
"...".writeln;
foreach (immutable date; fiveWeekendMonths[$ - 5 .. $])
writeln(date.month, ' ', date.year);
writeln("\nTotal number of years with no 5-weekend months: ",
totalNo5Weekends);

View file

@ -0,0 +1,26 @@
#lang racket
(require srfi/19)
(define long-months '(1 3 5 7 8 10 12))
(define days #(sun mon tue wed thu fri sat))
(define (week-day date)
(vector-ref days (date-week-day date)))
(define (five-weekends-a-month start end)
(for*/list ([year (in-range start (+ end 1))]
[month long-months]
[date (in-value (make-date 0 0 0 0 31 month year 0))]
#:when (eq? (week-day date) 'sun))
date))
(define weekends (five-weekends-a-month 1900 2100))
(define count (length weekends))
(displayln (~a "There are " count " weeks with five weekends."))
(displayln "The first five are: ")
(for ([w (take weekends 5)])
(displayln (date->string w "~b ~Y")))
(displayln "The last five are: ")
(for ([w (drop weekends (- count 5))])
(displayln (date->string w "~b ~Y")))

View file

@ -1,11 +1,13 @@
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.
# if the last day of the month falls on a Sunday and the month has 31 days,
# this is the only case where the month has 5 weekends.
start = Date.parse("1900-01-01")
stop = Date.parse("2100-12-31")
dates = (start..stop).find_all do |day|
day.mday == 31 and day.wday == 0 # Ruby 1.9: and day.sunday?
dates = []
1900.upto(2100) do |year|
1.upto(12) do |month|
d = Date.new(year, month, -1) # -1 is last day of month
dates << d if d.sunday? && d.day == 31
end
end
puts "There are #{dates.size} months with 5 weekends from 1900 to 2100:"

View file

@ -0,0 +1,12 @@
echo "Creating cal-file..."
echo > cal.txt
for ((y=1900; y <= 2100; y++)); do
for ((m=1; m <= 12; m++)); do
#echo $m $y
cal -m $m $y >> cal.txt
done
done
ls -la cal.txt
echo "Looking for month with 5 weekends:"
awk -f 5weekends.awk cal.txt

View file

@ -1,7 +1,7 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
proc WeekDay(Year, Month, Day); \Return day of week (0=Sat 1=Sun..6=Fri)
func WeekDay(Year, Month, Day); \Return day of week (0=Sat 1=Sun..6=Fri)
int Year, Month, Day;
[if Month<=2 then [Month:= Month+12; Year:= Year-1];
return rem((Day + (Month+1)*26/10 + Year + Year/4 + Year/100*6 + Year/400) / 7);