-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmondays.rb
More file actions
84 lines (72 loc) · 1.11 KB
/
mondays.rb
File metadata and controls
84 lines (72 loc) · 1.11 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'date'
def offsets_normal
offsets = []
offset = 0
[
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
].each do |days|
offsets << ((7 - offset) % 7)
offset = (offset + days) % 7
end
offsets
end
def offsets_leap
offsets = []
offset = 0
[
31, 29, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
].each do |days|
offsets << ((7 - offset) % 7)
offset = (offset + days) % 7
end
offsets
end
def sundays(start_year, end_year)
e = end_year
y = start_year
n = {}
l = {}
while y <= e
s = Date.parse("#{y}-01-01").wday
begin
Date.parse("#{y}-02-29").wday
l[s] ||= 0
l[s] += 1
rescue
n[s] ||= 0
n[s] += 1
end
y += 1
end
c = 0
offsets_normal.each do |i|
c += n[i] if n[i]
end
offsets_leap.each do |i|
c += l[i] if l[i]
end
c
end
sundays 1901, 2000
# sundays 2000, 2000
# 171
def long(start_year, end_year)
s = start_year
c = 0
while s <= end_year
for i in 1..12
if Date.parse("#{s}-#{i}-01").wday == 0
c += 1
end
end
s += 1
end
c
end
long 1901, 2000