473,805 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is the a problem with this code

thatos
105 New Member
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,t uesday,wednesda y,thursday,frid ay 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,.....,F riday.
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 1411
bvdet
2,851 Recognized Expert Moderator Specialist
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,t uesday,wednesda y,thursday,frid ay 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,.....,F riday.
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

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

Similar topics

1
5374
by: George | last post by:
Hi, I am trying to write a query in Oracle which I have not done before, and are having some difficulty getting my result. Please check my query and my results. select max(note.datetime), acgr.group_code, bank.local_acc_no, bank.short_name,
6
6788
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used the SQL Profile to watch the T-SQL-Command which Access ( who creates the commands?) creates and noticed:
3
8105
by: Memduh Durmaz | last post by:
Hi, I'm using DB2 UDB 7.2. Also I'm doing some tests on SQL Server 2000 for some statements to use efectively. I didn't find any solution on Sql Server about WITH ... SELECT structure of DB2. Is there any basic structure on Sql Server like WITH ... SELECT structure?
99
6257
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
27
2280
by: C Gillespie | last post by:
Dear All, Hopefully I have a simple problem. Basically, I just want to alter some text with JS. Here is some of my test code: <snip> <script type="text/javascript"> var tmp='a';
0
2705
by: Norm Wong | last post by:
If anyone is interested in using db2uext2 with Cygwin gcc compiler on Windows, I've modified the IBM provided sample with the attached file. There are two main modifications. The mkdir command is the POSIX compliant version as opposed to the MicroSoft C compiler. The second parameter to mkdir is the file mode. In this case, I've made the directories created to be read/write/execute(list) for all users.
3
6324
by: ChrisWinterscheid | last post by:
We are running DB2 8.1 on AIX 5.2. DB2level shows: DB21085I Instance "db2inst1" uses "32" bits and DB2 code release "SQL08016" with level identifier "02070106". Informational tokens are "DB2 v8.1.1.56", "s040616", "U497635", and FixPak "6". Product is installed at "/usr/opt/db2_08_01".
17
4234
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset Set rs = Me.RecordsetClone rs.Find "=" & lngContractID If Not rs.EOF Then Me.Bookmark = rs.Bookmark I must site the Heisenberb Uncertainty Principal here, as it
5
4075
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each employee based on their region, zone, job code and avg job time. (See code below). My problem is that I do not know how to rank the ties. Right now if two people have the same avg time one will be ranked 3rd and the other ranked 4th. I would...
7
4830
by: Skybuck Flying | last post by:
Hi, if (__builtin_expect (!buffer->__init, 0)) { buffer->__a = 0x5deece66dull; buffer->__c = 0xb; buffer->__init = 1; } I am using visual c 6 and this code doesn't compile, it's probably not
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.