challenge LL Golf Hole 3 - 13日の金曜日を数え上げる

今日から2013年12月31日までの、13日の金曜日とその総数を表示してください。

余力のあるものはこのプログラムを短くしてみたり、短くしてみたり、短くしてください。

※LL Future実行委員の高野光弘です。この出題は LL Future公式の出題であり、優れたものについてはLL Golfのセッションでご紹介させていただくかもしれません。ご理解の上、ご投稿ください。また、LL Futureのチケットは現在も発売中です。よろしければ、メインイベントの方にもぜひご参加ください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env ruby
require 'date'

from = DateTime.now
to = DateTime.parse("2013-12-31")

friday = (from..to).inject(0) do |friday, date|
    if date.mday == 13 and date.wday == 5 then
        puts date.strftime('%Y-%m-%d')
        friday + 1
    else
        friday
    end
end

puts friday

Posted feedbacks - Python

とりあえずナイーブなものを。

1
2
3
4
5
6
from datetime import*
d,n=date.today(),0
while d.year<2014:
 if d.day==13 and d.weekday()==4:print d;n+=1
 d+=timedelta(1)
print n

アフィリエイトが無いのが残念。

http://www.amazon.co.jp/gp/product/B000FPV7T8/

 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
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# vim: syntax=python

import datetime

def is_jason_friday(d):
  assert isinstance(d, datetime.date)
  return d.day == 13 and d.weekday() == 4

def next_friday(d):
  assert isinstance(d, datetime.date)
  for delta in range(1, 8):
    next = d + datetime.timedelta(delta)
    if next.weekday() == 4:
      return next

def friday_generator(start, end):
  assert isinstance(start, datetime.date)
  assert isinstance(end, datetime.date)
  d = start
  while d.toordinal() < end.toordinal():
    d = next_friday(d)
    if not d:
      break
    yield d

start = datetime.date.today()
end = datetime.date(2013, 12, 31)
count = 0
for d in friday_generator(start, end):
  if is_jason_friday(d):
    count+=1
    print d
print count

バグの修正

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
----  before
  d = start
  while d.toordinal() < end.toordinal():
    d = next_friday(d)
    if not d:
      break
    yield d

---- after
  d = next_friday(start)
  while d.toordinal() < end.toordinal():
    yield d
    d = next_friday(d)

datetime_rangeを用意して
a) 来週の金曜日
b) 次の月の13日
をもとめるやりかた両方をかけるようにした。
 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
# -*- coding: us-ascii -*-
# vim: syntax=python

import datetime

def is_jason_friday(d):
  assert isinstance(d, datetime.date)
  return d.day == 13 and d.weekday() == 4

def next_friday(d):
  assert isinstance(d, datetime.date)
  for delta in range(7, 0, -1):
    next = d + datetime.timedelta(delta)
    if next.weekday() == 4:
      return next

def next_13th_of_month(d):
  if d.day < 13:
    return d.replace(day=13)
  else:
    assert d.day >= 13
    e = d + datetime.timedelta(30)
    return e.replace(day=13)

def datetime_range(start, end, succ):
  assert isinstance(start, datetime.date)
  assert isinstance(end, datetime.date)
  d = succ(start)
  assert isinstance(d, datetime.date)
  while d.toordinal() < end.toordinal():
    yield d
    d = succ(d)

start = datetime.date.today()
end = datetime.date(2013, 12, 31)
count = 0
#for d in datetime_range(start, end, next_friday):
for d in datetime_range(start, end, next_13th_of_month):
  if is_jason_friday(d):
    count+=1
    print d
print count

Index

Feed

Other

Link

Pathtraq

loading...