473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

zellers congrunence

1. How do I keep prompting the user for more dates until the user has indicated that he/she wishes to quit.
2. Computation of the day of the week MUST be moved to its own function.
3. How do I verify that the date entered is valid. For example, April only has 30 days so the user will not be allowed to enter April 31 as a valid date. See input section below to see how this is to be accomplished.
4. I have 2 functions which will help me ensure that the user enters a valid date. How do I integrate these functions into your solution.

# dictionary of day names for output.
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
5:"Thursday", 6:"Friday"}

# Get user input: month, day, year
sys.stdout.write("Please enter the day of the month (1-31): ")
dayOfMonth = int(sys.stdin.readline().strip())

sys.stdout.write("Please enter the month (1-12): ")
month = int(sys.stdin.readline().strip())

sys.stdout.write("Please enter the year: ")
year = int(sys.stdin.readline().strip())

# Make adjustments for forumla
if month == 1 or month == 2:
month = month + 12
year = year - 1

# Compute coefficients
century = year/100
yearInCentury = year % 100

# Compute the day using Zeller's congruence
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
(yearInCentury/4) + (century/4) - 2*century) % 7

# Output answer
print "Day of week is %s" % dayNames[dayOfWeek]
Oct 31 '06 #1
12 7522
bartonc
6,596 Expert 4TB
Please post back using [ code ] [ /code ] (but without spaces) tags around your code. Otherwise, we can't see the structure of you code. Thanks.

for example
Expand|Select|Wrap|Line Numbers
  1.  
  2. # this is code
  3.  
Oct 31 '06 #2
here is the code...
# dictionary of day names for output.
dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
5:"Thursday", 6:"Friday"}

# Get user input: month, day, year
sys.stdout.write("Please enter the day of the month (1-31): ")
dayOfMonth = int(sys.stdin.readline().strip())

sys.stdout.write("Please enter the month (1-12): ")
month = int(sys.stdin.readline().strip())

sys.stdout.write("Please enter the year: ")
year = int(sys.stdin.readline().strip())

# Make adjustments for forumla
if month == 1 or month == 2:
month = month + 12
year = year - 1

# Compute coefficients
century = year/100
yearInCentury = year % 100

# Compute the day using Zeller's congruence
dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
(yearInCentury/4) + (century/4) - 2*century) % 7

# Output answer
print "Day of week is %s" % dayNames[dayOfWeek]
Nov 1 '06 #3
bartonc
6,596 Expert 4TB
I'll work on the date thing. I want you to do the loop based on your other post, OK?
Nov 1 '06 #4
bartonc
6,596 Expert 4TB
Here's some date stuff for you to play with. Also look at the datetime module in the standard library.
Expand|Select|Wrap|Line Numbers
  1. import calendar
  2. year = 2006
  3. month = 10
  4. day = 31
  5. calendar.setfirstweekday(calendar.SUNDAY)
  6. fd, nDays = calendar.monthrange(year, month)
  7. if day > nDays:
  8.     print "not a valid date"
  9.  
Please use [code] tags when you post back. Thanks,
Barton
Nov 2 '06 #5
Expand|Select|Wrap|Line Numbers
  1.  Input: the user inputs the amount in one of the following forms:
  2. #         C value
  3. #         F value
  4. #         CM value
  5. #         I value
  6. #
  7. # C - is a celcius value.  Program converts to fahrenheit
  8. # F - is a fahrenheit value.  Program converts to celcius
  9. # CM - is a centimetre value.  Program converts to inches
  10. # I - is an inch value.  Program converts to centimetres
  11.  
  12. import sys
  13.  
  14. # Prompt user
  15. sys.stdout.write( "Enter a value to convert: ")
  16.  
  17. # Read in a line from the keyboard
  18. lineElements = sys.stdin.readline().split(" ")
  19.  
  20. # If the value is Celcius, convert to Fahrenheit
  21. if lineElements[0].lower() == 'c':
  22.     print "%s degress celcius is %.2f degrees fahrenheit." \
  23.           % ( lineElements[1].strip(), (float(lineElements[1])*9.0/5.0)+32)
  24.  
  25. # If the value is Fahrenheit, convert to Celcius
  26. elif lineElements[0].lower() == 'f':
  27.     print "%s degress fahrenheit is %.2f degrees celcius." \
  28.           % ( lineElements[1].strip(), (float(lineElements[1]) - 32) * 5.0/9.0)
  29.  
  30. # if the value is in centimetres, convert to inches
  31. elif lineElements[0].lower() == 'cm':
  32.     print "%s centimetres is %.2f inches." % ( lineElements[1].strip(),
  33.                                              (float(lineElements[1]) * 0.3937))
  34.  
  35. # if the value is in inches, convert to centimetres
  36. elif lineElements[0].lower() == 'i':
  37.     print "%s inches is %.2f centimetres." % ( lineElements[1].strip(),
  38.                                              (float(lineElements[1]) * 2.54))
Nov 3 '06 #6
Sorry wrong code...this is the right code

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # dictionary of day names for output.
  4. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
  5.  5:"Thursday", 6:"Friday"}
  6.  
  7. # Get user input:  month, day, year
  8. sys.stdout.write("Please enter the day of the month (1-31): ")
  9. dayOfMonth = int(sys.stdin.readline().strip())
  10.  
  11. sys.stdout.write("Please enter the month (1-12): ")
  12. month = int(sys.stdin.readline().strip())
  13.  
  14. sys.stdout.write("Please enter the year: ")
  15. year = int(sys.stdin.readline().strip())
  16.  
  17. # Make adjustments for forumla 
  18. if month == 1 or month == 2:
  19.     month = month + 12
  20.     year = year - 1
  21.  
  22. # Compute coefficients
  23. century = year/100
  24. yearInCentury = year % 100
  25.  
  26. # Compute the day using Zeller's congruence
  27. dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
  28.              (yearInCentury/4) + (century/4) - 2*century) % 7
  29.  
  30. # Output answer
  31. print "Day of week is %s" % dayNames[dayOfWeek]
Nov 3 '06 #7
bartonc
6,596 Expert 4TB
Sorry wrong code...this is the right code

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # dictionary of day names for output.
  4. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
  5. 5:"Thursday", 6:"Friday"}
  6.  
  7. # Get user input: month, day, year
  8. sys.stdout.write("Please enter the day of the month (1-31): ")
  9. dayOfMonth = int(sys.stdin.readline().strip())
  10.  
  11. sys.stdout.write("Please enter the month (1-12): ")
  12. month = int(sys.stdin.readline().strip())
  13.  
  14. sys.stdout.write("Please enter the year: ")
  15. year = int(sys.stdin.readline().strip())
  16.  
  17. # Make adjustments for forumla 
  18. if month == 1 or month == 2:
  19. month = month + 12
  20. year = year - 1
  21.  
  22. # Compute coefficients
  23. century = year/100
  24. yearInCentury = year % 100
  25.  
  26. # Compute the day using Zeller's congruence
  27. dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
  28. (yearInCentury/4) + (century/4) - 2*century) % 7
  29.  
  30. # Output answer
  31. print "Day of week is %s" % dayNames[dayOfWeek]
Awesome! You can post using tags! That's great.
Now you have some more things to work on after you copy the pieces from my last posts.
You CAN copy and paste to and from the forum, right?
Nov 3 '06 #8
I am trying to incorporate this code into this program but I am have trouble getting to run properly. These functions are used for leap years...

Expand|Select|Wrap|Line Numbers
  1. def isLeap(year):
  2.     if year % 400 == 0:
  3.         return True
  4.     elif year % 4 == 0 and year % 100 !=0:
  5.         return True
  6.     else:
  7.         return False
  8.  
  9. def computeMaxDay(month, year):
  10.     maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  11.     if month == 2:
  12.         if isLeap(year):
  13.             return 29
  14.     return maxDays[month]
Nov 6 '06 #9
bartonc
6,596 Expert 4TB
I am trying to incorporate this code into this program but I am have trouble getting to run properly. These functions are used for leap years...

Expand|Select|Wrap|Line Numbers
  1. def isLeap(year):
  2. if year % 400 == 0:
  3. return True
  4. elif year % 4 == 0 and year % 100 !=0:
  5. return True
  6. else:
  7. return False
  8.  
  9. def computeMaxDay(month, year):
  10. maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  11. if month == 2:
  12. if isLeap(year):
  13. return 29
  14. return maxDays[month]
since

>>> 1996 %4
0
>>> 1997 %4
1
>>> 2000 %4
0
>>> not(2008 % 4)
True
>>>
You can simply
Expand|Select|Wrap|Line Numbers
  1. def IsLeapYear(year):
  2.     return not (year % 4)
  3.  
Nov 6 '06 #10
I am to follow that exact code and not change it...this is what I have got but I am having trouble getting it to work.

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # dictionary of day names for output.
  4. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
  5. 5:"Thursday", 6:"Friday"}
  6.  
  7. # Get user input: month, day, year
  8. sys.stdout.write("Please enter the day of the month (1-31): ")
  9. dayOfMonth = int(sys.stdin.readline().strip())
  10.  
  11. sys.stdout.write("Please enter the month (1-12): ")
  12. month = int(sys.stdin.readline().strip())
  13.  
  14. sys.stdout.write("Please enter the year: ")
  15. year = int(sys.stdin.readline().strip())
  16. # Determining Leap year.
  17. def isLeap(year):
  18.     if year % 400 == 0:
  19.         return True
  20.     elif year % 4 == 0 and year % 100 !=0:
  21.         return True
  22.     else:
  23.         return False
  24. # Determining Maximum number of days in each month possible.
  25. def computeMaxDay(month, year):
  26.     maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  27.     if month == 2:
  28.         if isLeap(year):
  29.             return 29
  30.     return maxDays[month]
  31.  
  32. # Make adjustments for forumla 
  33. if month == 1 or month == 2:
  34. month = month + 12
  35. year = year - 1
  36.  
  37. # Compute coefficients
  38. century = year/100
  39. yearInCentury = year % 100
  40.  
  41. # Compute the day using Zeller's congruence
  42. dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
  43. (yearInCentury/4) + (century/4) - 2*century) % 7
  44.  
  45.  
  46.  
  47. # Output answer
  48. print "Day of week is %s" % dayNames[dayOfWeek]
  49.  
  50. # Output Quit Program  
  51. while(input != "quit\n"):
  52.  input = sys.stdin.readline()
  53.  print "you typed : " + input
since

>>> 1996 %4
0
>>> 1997 %4
1
>>> 2000 %4
0
>>> not(2008 % 4)
True
>>>
You can simply
Expand|Select|Wrap|Line Numbers
  1. def IsLeapYear(year):
  2.     return not (year % 4)
  3.  
Nov 6 '06 #11
bartonc
6,596 Expert 4TB
This ought to help. Let us know how this ends up, please.
Expand|Select|Wrap|Line Numbers
  1.  
  2. import sys
  3.  
  4. # dictionary of day names for output.
  5. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
  6. 5:"Thursday", 6:"Friday"}
  7. # Determining Leap year.
  8. def isLeap(year):
  9.     if year % 400 == 0:
  10.         return True
  11.     elif year % 4 == 0 and year % 100 !=0:
  12.         return True
  13.     else:
  14.         return False
  15. # Determining Maximum number of days in each month possible.
  16. def computeMaxDay(month, year):
  17.     maxDays = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  18.     if month == 2 and isLeap(year):
  19.         return 29
  20.     return maxDays[month]
  21. def Zellers(day, month, year):
  22.     # Make adjustments for forumla 
  23.     if month == 1 or month == 2:
  24.         month = month + 12
  25.     year = year - 1
  26.  
  27.     # Compute coefficients
  28.     century = year/100
  29.     yearInCentury = year % 100
  30.  
  31.     # Compute the day using Zeller's congruence
  32.     dayOfWeek = (day + ((month + 1)* 26)/10 + yearInCentury +
  33.     (yearInCentury/4) + (century/4) - 2*century) % 7
  34.     # Output answer
  35.     return dayNames[dayOfWeek]
  36.  
  37.     # Your post says that you are not to change the code
  38.     # But your code needs to change here so that you can prevent errors
  39.     # Because "quit\n" can't be converted to an int.
  40.     # but here is a "brute force" way to do it
  41. print "Enter 'q' to quit" # anything other than a number works
  42. while True:
  43.     try:
  44.         # Get user input: month, day, year
  45.         sys.stdout.write("Please enter the day of the month (1-31): ")
  46.         dayOfMonth = int(sys.stdin.readline().strip())
  47.         sys.stdout.write("Please enter the month (1-12): ")
  48.         month = int(sys.stdin.readline().strip())
  49.         if month > 12:
  50.             print "whatever"
  51.             continue    # Bounce back to top of loop
  52.  
  53.         sys.stdout.write("Please enter the year: ")
  54.         year = int(sys.stdin.readline().strip())
  55.  
  56.         if dayOfMonth > computeMaxDay(month, year):
  57.             print "whatever"
  58.             continue    # Bounce back to top of loop
  59.         dayName = Zellers(dayOfMonth, month, year)
  60.         print "Day of week is %s" % dayName
  61.         print
  62.     except ValueError:
  63.         break
  64.  
Keep posting,
Barton
Nov 7 '06 #12
I know this thread is old but.. its was just what I needed. Thanks for being there. I incorporated your bits parts in my script along with a little extra flair. Here have a look. Thanks again.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python2.5
  2. import os,sys
  3.  
  4. #print out months of the year
  5.  
  6. print "Please type in your birthdate by the year, month and day."
  7.  
  8. months = [
  9.     'Janurary',
  10.     'February',
  11.     'March',
  12.     'April',
  13.     'May',
  14.     'June',
  15.     'July',
  16.     'August',
  17.     'September',
  18.     'October',
  19.     'November',
  20.     'December' 
  21. ]
  22.  
  23. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday", 5:"Thursday", 6:"Friday"}
  24.  
  25. #Ending is just a seriies of additions of the endings. Instead of listing them all IE 'th', 'th'
  26. # the mulitplicaitons does it for us and keeps it short for reading purposes
  27. # IE.    'th, 'th, 'th"   is equal  to  3 * ['th']    in the code below  you could use the paranethsis
  28. # or not as the mulitplication happen 1st in all code then the additions. 
  29.  
  30. endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
  31.         + ['st', 'nd', 'rd'] +  7 * ['th'] \
  32.         + ['st']
  33.  
  34. year = int(raw_input('Year: '))
  35. month = int(input('Month (1-12):'))
  36.  
  37. if month <= 12  and month >= 1:
  38.     pass
  39. else:
  40.     sys.exit(`month` + " is not between 1-12")
  41.  
  42. dayOfMonth =int(input('Day (1-31): '))
  43.  
  44. #if day <= 31  and day >= 1:
  45. #    print " "
  46. #else:
  47. #    sys.exit(`day` + " is not between 1-12")
  48.  
  49.  
  50. month_number = int(month)
  51. day_number   = int(dayOfMonth)
  52.  
  53. month_name = months[month_number-1]
  54. ordinal = `dayOfMonth` + endings[day_number -1]
  55.  
  56.  
  57. if month == 1 or month == 2:
  58.     month  = month + 12
  59.     year = year - 1
  60.  
  61. century = year/100
  62. yearInCentury = year % 100
  63.  
  64.  
  65. print dayOfMonth
  66. dayOfWeek = (dayOfMonth + ((month +1) * 26) /10 + yearInCentury + (yearInCentury/4) + (century/4) - 2*century) % 7
  67.  
  68.  
  69. print  month_name + " " + ordinal + "," + "%s is a %s" % (year, dayNames[dayOfWeek]) 
  70.  
  71.  
  72.  
Jun 11 '12 #13

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

Similar topics

6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
5
by: bratiskovci | last post by:
Hello, I am trying to write a few programs using python but don't really know where to start...I am completely CONFUSED. My first program deals with conversion between fahrenheit and celcius...
2
by: bratiskovci | last post by:
I have to write a genetics program: This is the INPUT: 25 sets of genetic sequences are provided. Each sample is provided in a specific format so that my program may read in the genetic...
9
by: iHateProg | last post by:
I am looking for a day of birth generator that will use MONTH DATE AND YEAR born to generate the BIRTH DAY from 1900-2000. Ex:- If someone was born on April 10, 1990. what day of the week were they...
1
by: ntraviglia | last post by:
Can anyone help? I have to write a zellers congruence program with visual. Anyone have any sample code or maybe the actual zeller program in visual they can give me, I am totally lost.
12
by: Justn226 | last post by:
anyone have any idea why i am not getting any return values? it will return the words just not the numbers? HTML <HTML> <head> <title>Zellers Carpeting Cost Estimate</title> </head> ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.