3

I am trying to count the number of Friday the 13ths per year from 1950-2050 using Python (I know, a little late). I am not familiar with any date/calendar packages to use. Any thoughts?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
mike
  • 22,931
  • 31
  • 77
  • 100

6 Answers6

7

This has a direct solution. Use sum to count the number of times where the 13th of the month is a Friday:

>>> from datetime import datetime # the function datetime from module datetime
>>> sum(datetime(year, month, 13).weekday() == 4 
        for year in range(1950, 2051) for month in range(1,13))
174
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
4

the datetime.date class has a weekday() function that gives you the day of the week (indexed from 0) as an integer, so Friday is 4. There's also isoweekday() that indexes days from 1, it's up to you which you prefer.

Anyway, a simple solution would be:

friday13 = 0
months = range(1,13)
for year in xrange(1950, 2051):
    for month in months:
        if date(year, month, 13).weekday() == 4:
            friday13 += 1
Endophage
  • 21,038
  • 13
  • 59
  • 90
  • I believe `months = range(0, 13)` could be simplified to `months = range(13)` – Nolen Royalty Apr 16 '12 at 04:01
  • @NolenRoyalty not my homework and when I was in college, we had plenty of older students around that were paid to be on hand to help. Good point on the range though... I'm typically highly explicit out of habit but that's one that doesn't really need the `0`. – Endophage Apr 16 '12 at 04:03
  • 1
    Not your homework, but the [suggested way to deal with homework](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions) is relevant. – Nolen Royalty Apr 16 '12 at 04:05
  • Also I believe this actually throws an error, you need range(1, 13) because month must be at least 1... – Nolen Royalty Apr 16 '12 at 04:07
  • @NolenRoyalty :-P you're right on the month thing. Should start from 1. Homework *shrug* – Endophage Apr 16 '12 at 04:08
0

Is it some kind of exercise or homework? I faintly remember of having solved it. I can give you a hint, I seem to have used Calendar.itermonthdays2 Of course there should be other ways to solve it as well.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • I've seen bugs with iterating months in Python. Things like jumping back one month from March 1st. OK, technically not iterating, but I'm just saying, it's dodgy to rely on time manipulation over months, there is no fixed definition of the time period that makes up "1 month" and subsequently it's not implemented quite right in all cases... – Endophage Apr 16 '12 at 04:06
0

Sounds like homework. Hint (weekday 4 is a Friday):

import datetime
print(datetime.datetime(1950,1,13).weekday())
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

While other solutions are clear and simple, the following one is more "calendarist". You will need the dateutil package, which is installable as a package:

from datetime import datetime
from dateutil import rrule

fr13s = list(rrule.rrule(rrule.DAILY,
                         dtstart=datetime(1950,1,13),
                         until=datetime(2050,12,13),
                         bymonthday=[13],
                         byweekday=[rrule.FR]))
# this returns a list of 174 datetime objects

You see these five arguments of rrule.rrule: Take every rrule.DAILY (day) between dtstart and until where bymonthday is 13 and byweekday is rrule.FR (Friday).

eumiro
  • 207,213
  • 34
  • 299
  • 261
0
from datetime import *
from time import strptime
yil = input("yilni kiritnig:: ")
kun1 = "01/01/"+yil
kun1 = datetime.strptime(kun1, "%d/%m/%Y")
while 1:
    if kun1.strftime("%A") == "Friday":
        break
    kun1 = kun1 + timedelta(days=1)
count = 0
while 1:
    if int(kun1.strftime("%Y")) == int(yil)+1:
        break
    if kun1.strftime("%d") == "13":
        count+=1
    kun1=kun1+timedelta(days=7)
print(count)
  • this is a little similir – Shohida Abdullayeva Sep 29 '22 at 11:45
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 17:33