Advertisement
hieuza

Python: Friday 13th

Jan 4th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # @hieuza [at] gmail.com
  3. # 04.Jan.2014
  4.  
  5. # the months with 13th Friday of a year
  6. import datetime
  7.  
  8. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  9. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  10. days_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  11.  
  12. # start_day: the weekday of 1st January.
  13. # 0: Monday,... 6: Sunday
  14. def friday13_month(days, start_day):
  15.     accum = [13]
  16.     for x in days[:-1]:
  17.         accum.append(accum[-1] + x)
  18.  
  19.     # weekday of day 13, if 01/Jan is on Monday (0)
  20.     # must minus 1, because day started at 1
  21.     day13_start0 = [(x - 1) % 7 for x in accum]
  22.  
  23.     # day 13, in considerate of the start day
  24.     day13 = [(x + start_day) % 7 for x in day13_start0]
  25.  
  26.     # which month has Friday 13
  27.     res = []
  28.     for i, x in enumerate(day13):
  29.         if x == 4: # Friday
  30.             res.append(i)
  31.  
  32.     return res
  33.  
  34. def is_leap_year(year):
  35.     return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0)
  36.  
  37. def which_month_friday13(year):
  38.     day1 = datetime.date(year, 1, 1)
  39.     start_day = day1.weekday()
  40.    
  41.     # print 'year:', year, 'start_day:', start_day
  42.  
  43.     if is_leap_year(year):
  44.         res = friday13_month(days_leap, start_day)
  45.     else:
  46.         res = friday13_month(days, start_day)
  47.  
  48.     print [months[i] for i in res]
  49.  
  50.     return res
  51.  
  52. def test_is_leap_year():
  53.     print '2000:', is_leap_year(2000)
  54.     print '1800:', is_leap_year(1800)
  55.     print '2008:', is_leap_year(2008)
  56.  
  57. def test_friday13(year):
  58.     # year = 2014
  59.     res = which_month_friday13(year)
  60.  
  61.     # check the correct Friday 13
  62.     for m in res:
  63.         d = datetime.date(year, m + 1, 13)
  64.         if d.weekday() != 4:
  65.             print 'ERROR: 13/%s is not Friday' % months[m]
  66.     # check that there's no other Friday 13
  67.     for m in xrange(12):
  68.         if m in res: continue
  69.         d = datetime.date(year, m + 1, 13)
  70.         if d.weekday() == 4:
  71.             print 'ERROR: 13th %s is Friday' % months[m]
  72.  
  73. def test_interactive():
  74.     print 'Enter a year to print the month with Friday 13. 0 to exit'
  75.  
  76.     while True:
  77.         year = int(raw_input('year: '))
  78.         if year == 0:
  79.             break
  80.  
  81.         test_friday13(year)
  82.  
  83. if __name__ == '__main__':
  84.     # test_is_leap_year()
  85.     # test_friday13(2014)
  86.     test_interactive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement