469,347 Members | 1,738 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

is the a problem with this code

thatos
105 100+
I was requested to make a code for the following question
write a program that will compute the frequency that the 13th of each month lands on sunday,monday,tuesday,wednesday,thursday,friday and saturday over a given period of N years.
input format: must read a single integer from file friday.in
output format: must write the following in file friday.out ; seven space separated integers on one line.These intergers represent the number of times the 13th falls on Saturday.Sunday,Monday,.....,Friday.
here is my python code:
Expand|Select|Wrap|Line Numbers
  1. def friday():
  2.     f = open("friday.in",'r')
  3.     out = open("friday.out",'w')
  4.     s = f.readline()
  5.                 ss = int(s[0])
  6.     day = 13
  7.     year = 1900
  8.     thirteenthcounter = {"monday":0,"tuesday":0,"wednesday":0,"thursday":0,"friday":0,"saturday":0,"sunday":0}
  9.     nummonths = {1:"january",2:"february",3:"march",4:"april",5:"may",6:"june",7:"july",8:"august",9:"september",10:"october",11:"november",12:"december"}
  10.     while (year < year + int(s[0])):
  11.         if (year % 4 == 0) or (year%100 == 0 and year%400 == 0):
  12.             months = {"january":31,"february":29,"march":31,"april":30,"may":31,"june":30,"july":31,"august":31,"september":30,"october":31,"november":30,"december":31}
  13.         else:
  14.             months = {"january":31,"february":28,"march":31,"april":30,"may":31,"june":30,"july":31,"august":31,"september":30,"october":31,"november":30,"december":31}
  15.         month = 1
  16.         while month <= 12:
  17.             if day % 7 == 1:
  18.                 thirteenthcounter["monday"] += 1
  19.             elif day % 7 == 2:
  20.                 thirteenthcounter["tuesday"] += 1
  21.             elif day % 7 == 3:
  22.                 thirteenthcounter["wednesday"] += 1
  23.             elif day % 7 == 4:
  24.                 thirteenthcounter["thursday"] += 1
  25.             elif day % 7 == 5:
  26.                 thirteenthcounter["friday"] += 1
  27.             elif day % 7 == 6:
  28.                 thirteenthcounter["saturday"] += 1
  29.             else:
  30.                 thirteenthcounter["sunday"] += 1
  31.             day += months[nummonths[month]]
  32.             month += 1
  33.         year += 1
  34.     out.write[thirteenthcounter["saturday"]+" "+thirteenthcounter["sunday"]+ " " + thirteenthcounter["monday"]+ " "+thirteenthcounter["tuesday"]+" "+thirteenthcounter["wednesday"]+" "+thirteenthcounter["thursday"]+" "+thirteenthcounter["friday"]+"\n"]
  35.     out.close()
  36.  
If I made some mistakes please tell me where they are and if the is not problem with the code please tell me what is the result when you have 20 years or try to make it shorter.
Feb 3 '08 #1
1 1153
bvdet
2,851 Expert Mod 2GB
I was requested to make a code for the following question
write a program that will compute the frequency that the 13th of each month lands on sunday,monday,tuesday,wednesday,thursday,friday and saturday over a given period of N years.
input format: must read a single integer from file friday.in
output format: must write the following in file friday.out ; seven space separated integers on one line.These intergers represent the number of times the 13th falls on Saturday.Sunday,Monday,.....,Friday.
here is my python code:
Expand|Select|Wrap|Line Numbers
  1. def friday():
  2.     f = open("friday.in",'r')
  3.     out = open("friday.out",'w')
  4.     s = f.readline()
  5.                 ss = int(s[0])
  6.     day = 13
  7.     year = 1900
  8.     thirteenthcounter = {"monday":0,"tuesday":0,"wednesday":0,"thursday":0,"friday":0,"saturday":0,"sunday":0}
  9.     nummonths = {1:"january",2:"february",3:"march",4:"april",5:"may",6:"june",7:"july",8:"august",9:"september",10:"october",11:"november",12:"december"}
  10.     while (year < year + int(s[0])):
  11.         if (year % 4 == 0) or (year%100 == 0 and year%400 == 0):
  12.             months = {"january":31,"february":29,"march":31,"april":30,"may":31,"june":30,"july":31,"august":31,"september":30,"october":31,"november":30,"december":31}
  13.         else:
  14.             months = {"january":31,"february":28,"march":31,"april":30,"may":31,"june":30,"july":31,"august":31,"september":30,"october":31,"november":30,"december":31}
  15.         month = 1
  16.         while month <= 12:
  17.             if day % 7 == 1:
  18.                 thirteenthcounter["monday"] += 1
  19.             elif day % 7 == 2:
  20.                 thirteenthcounter["tuesday"] += 1
  21.             elif day % 7 == 3:
  22.                 thirteenthcounter["wednesday"] += 1
  23.             elif day % 7 == 4:
  24.                 thirteenthcounter["thursday"] += 1
  25.             elif day % 7 == 5:
  26.                 thirteenthcounter["friday"] += 1
  27.             elif day % 7 == 6:
  28.                 thirteenthcounter["saturday"] += 1
  29.             else:
  30.                 thirteenthcounter["sunday"] += 1
  31.             day += months[nummonths[month]]
  32.             month += 1
  33.         year += 1
  34.     out.write[thirteenthcounter["saturday"]+" "+thirteenthcounter["sunday"]+ " " + thirteenthcounter["monday"]+ " "+thirteenthcounter["tuesday"]+" "+thirteenthcounter["wednesday"]+" "+thirteenthcounter["thursday"]+" "+thirteenthcounter["friday"]+"\n"]
  35.     out.close()
  36.  
If I made some mistakes please tell me where they are and if the is not problem with the code please tell me what is the result when you have 20 years or try to make it shorter.
Your logic looks sound. Here's a way to determine the frequency using the datetime module, dictionary method setdefault(), and a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. def day_freq(day_of_month, start_yr, end_yr):
  2.     weekdays = ['Monday', 'Tuesday', 'Wednesday', \
  3.                 'Thursday','Friday', 'Saturday', 'Sunday']
  4.     weekdayDict = dict(zip(range(0,7), weekdays))
  5.     dateList = [weekdayDict[datetime.date(y,m,day_of_month).weekday()] \
  6.                 for m in range(1,13) for y in range(start_yr,end_yr)]    
  7.     dayDict = {}
  8.     for day in dateList:
  9.         dayDict[day] = dayDict.setdefault(day, 0)+1    
  10.     for day in weekdays:
  11.         print '%s occurred %d time%s.' % \
  12.               (day, dayDict.get(day, 0), ['', 's'][dayDict[day]>1 or 0])
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> day_freq(1, 1951, 1952)
  2. Monday occurred 2 times.
  3. Tuesday occurred 1 time.
  4. Wednesday occurred 1 time.
  5. Thursday occurred 3 times.
  6. Friday occurred 1 time.
  7. Saturday occurred 2 times.
  8. Sunday occurred 2 times.
  9. >>> 
Feb 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

27 posts views Thread by C Gillespie | last post: by
reply views Thread by Norm Wong | last post: by
3 posts views Thread by ChrisWinterscheid | last post: by
7 posts views Thread by Skybuck Flying | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.