Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The problem You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All t...
#3: Post edited
Counting Sundays without Python date module
- Counting Sundays without Python datetime module
- # The problem
- > You are given the following information, but you may prefer to do some
- > research for yourself.
- >
- > * 1 Jan 1900 was a Monday.
- > * Thirty days has September, April, June and November. All the rest have thirty-one, saving February alone, which has twenty-eight, rain
- > or shine.
- > * And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
- >
- > How many Sundays fell on the first of the month during the twentieth
- > century (1 Jan 1901 to 31 Dec 2000)?
- # The solution
- ```
- def is_century(year):
- return year % 100 == 0
- def is_leap_year(year):
- if is_century(year):
- return year % 400 == 0
- else:
- return year % 4 == 0
- def get_day_count(year, month):
- if month == 2 and is_leap_year(year):
- return 29
- elif month == 2:
- return 28
- elif month in [4, 6, 9, 11]:
- return 30
- else:
- return 31
- def solution(start_day, start_month, start_year, end_day, end_month, end_year):
- current_day, current_month, current_year = start_day, start_month, start_year
- days = 0
- sundays = 0
- while current_day != end_day or current_month != end_month or current_year != end_year:
- if days % 7 == 0 and current_day == 1:
- sundays += 1
- days += 1
- if current_day == get_day_count(current_year, current_month):
- current_day = 1
- if current_month == 12:
- current_month = 1
- current_year += 1
- else:
- current_month += 1
- else:
- current_day += 1
- return sundays
- print(solution(1, 1, 1901, 31, 12, 2000))
- ```
I solved Project Euler Problem 19 with Python. My goal was to solve this without the `date` module, and handling the calendar part on my own.- As the problem states the start date is a Monday, I counted the next Mondays that fell on the first of the month using this condition: `if days % 7 == 0 and current_day == 1:`, being `days` the number of days I counted so far inside the loop.
- What can be improved? Have I overcomplicated some part of the code?
- # The problem
- > You are given the following information, but you may prefer to do some
- > research for yourself.
- >
- > * 1 Jan 1900 was a Monday.
- > * Thirty days has September, April, June and November. All the rest have thirty-one, saving February alone, which has twenty-eight, rain
- > or shine.
- > * And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
- >
- > How many Sundays fell on the first of the month during the twentieth
- > century (1 Jan 1901 to 31 Dec 2000)?
- # The solution
- ```
- def is_century(year):
- return year % 100 == 0
- def is_leap_year(year):
- if is_century(year):
- return year % 400 == 0
- else:
- return year % 4 == 0
- def get_day_count(year, month):
- if month == 2 and is_leap_year(year):
- return 29
- elif month == 2:
- return 28
- elif month in [4, 6, 9, 11]:
- return 30
- else:
- return 31
- def solution(start_day, start_month, start_year, end_day, end_month, end_year):
- current_day, current_month, current_year = start_day, start_month, start_year
- days = 0
- sundays = 0
- while current_day != end_day or current_month != end_month or current_year != end_year:
- if days % 7 == 0 and current_day == 1:
- sundays += 1
- days += 1
- if current_day == get_day_count(current_year, current_month):
- current_day = 1
- if current_month == 12:
- current_month = 1
- current_year += 1
- else:
- current_month += 1
- else:
- current_day += 1
- return sundays
- print(solution(1, 1, 1901, 31, 12, 2000))
- ```
- I solved Project Euler Problem 19 with Python. My goal was to solve this without the `datetime` module, and handling the calendar part on my own.
- As the problem states the start date is a Monday, I counted the next Mondays that fell on the first of the month using this condition: `if days % 7 == 0 and current_day == 1:`, being `days` the number of days I counted so far inside the loop.
- What can be improved? Have I overcomplicated some part of the code?
#2: Post edited
Counting Sundays without Python date module
# The probelm- > You are given the following information, but you may prefer to do some
- > research for yourself.
- >
- > * 1 Jan 1900 was a Monday.
- > * Thirty days has September, April, June and November. All the rest have thirty-one, saving February alone, which has twenty-eight, rain
- > or shine.
- > * And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
- >
- > How many Sundays fell on the first of the month during the twentieth
- > century (1 Jan 1901 to 31 Dec 2000)?
- # The solution
- ```
- def is_century(year):
- return year % 100 == 0
- def is_leap_year(year):
- if is_century(year):
- return year % 400 == 0
- else:
- return year % 4 == 0
- def get_day_count(year, month):
- if month == 2 and is_leap_year(year):
- return 29
- elif month == 2:
- return 28
- elif month in [4, 6, 9, 11]:
- return 30
- else:
- return 31
- def solution(start_day, start_month, start_year, end_day, end_month, end_year):
- current_day, current_month, current_year = start_day, start_month, start_year
- days = 0
- sundays = 0
- while current_day != end_day or current_month != end_month or current_year != end_year:
- if days % 7 == 0 and current_day == 1:
- sundays += 1
- days += 1
- if current_day == get_day_count(current_year, current_month):
- current_day = 1
- if current_month == 12:
- current_month = 1
- current_year += 1
- else:
- current_month += 1
- else:
- current_day += 1
- return sundays
- print(solution(1, 1, 1901, 31, 12, 2000))
- ```
- I solved Project Euler Problem 19 with Python. My goal was to solve this without the `date` module, and handling the calendar part on my own.
- As the problem states the start date is a Monday, I counted the next Mondays that fell on the first of the month using this condition: `if days % 7 == 0 and current_day == 1:`, being `days` the number of days I counted so far inside the loop.
- What can be improved? Have I overcomplicated some part of the code?
- # The problem
- > You are given the following information, but you may prefer to do some
- > research for yourself.
- >
- > * 1 Jan 1900 was a Monday.
- > * Thirty days has September, April, June and November. All the rest have thirty-one, saving February alone, which has twenty-eight, rain
- > or shine.
- > * And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
- >
- > How many Sundays fell on the first of the month during the twentieth
- > century (1 Jan 1901 to 31 Dec 2000)?
- # The solution
- ```
- def is_century(year):
- return year % 100 == 0
- def is_leap_year(year):
- if is_century(year):
- return year % 400 == 0
- else:
- return year % 4 == 0
- def get_day_count(year, month):
- if month == 2 and is_leap_year(year):
- return 29
- elif month == 2:
- return 28
- elif month in [4, 6, 9, 11]:
- return 30
- else:
- return 31
- def solution(start_day, start_month, start_year, end_day, end_month, end_year):
- current_day, current_month, current_year = start_day, start_month, start_year
- days = 0
- sundays = 0
- while current_day != end_day or current_month != end_month or current_year != end_year:
- if days % 7 == 0 and current_day == 1:
- sundays += 1
- days += 1
- if current_day == get_day_count(current_year, current_month):
- current_day = 1
- if current_month == 12:
- current_month = 1
- current_year += 1
- else:
- current_month += 1
- else:
- current_day += 1
- return sundays
- print(solution(1, 1, 1901, 31, 12, 2000))
- ```
- I solved Project Euler Problem 19 with Python. My goal was to solve this without the `date` module, and handling the calendar part on my own.
- As the problem states the start date is a Monday, I counted the next Mondays that fell on the first of the month using this condition: `if days % 7 == 0 and current_day == 1:`, being `days` the number of days I counted so far inside the loop.
- What can be improved? Have I overcomplicated some part of the code?
#1: Initial revision
Counting Sundays without Python date module
# The probelm > You are given the following information, but you may prefer to do some > research for yourself. > > * 1 Jan 1900 was a Monday. > * Thirty days has September, April, June and November. All the rest have thirty-one, saving February alone, which has twenty-eight, rain > or shine. > * And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. > > How many Sundays fell on the first of the month during the twentieth > century (1 Jan 1901 to 31 Dec 2000)? # The solution ``` def is_century(year): return year % 100 == 0 def is_leap_year(year): if is_century(year): return year % 400 == 0 else: return year % 4 == 0 def get_day_count(year, month): if month == 2 and is_leap_year(year): return 29 elif month == 2: return 28 elif month in [4, 6, 9, 11]: return 30 else: return 31 def solution(start_day, start_month, start_year, end_day, end_month, end_year): current_day, current_month, current_year = start_day, start_month, start_year days = 0 sundays = 0 while current_day != end_day or current_month != end_month or current_year != end_year: if days % 7 == 0 and current_day == 1: sundays += 1 days += 1 if current_day == get_day_count(current_year, current_month): current_day = 1 if current_month == 12: current_month = 1 current_year += 1 else: current_month += 1 else: current_day += 1 return sundays print(solution(1, 1, 1901, 31, 12, 2000)) ``` I solved Project Euler Problem 19 with Python. My goal was to solve this without the `date` module, and handling the calendar part on my own. As the problem states the start date is a Monday, I counted the next Mondays that fell on the first of the month using this condition: `if days % 7 == 0 and current_day == 1:`, being `days` the number of days I counted so far inside the loop. What can be improved? Have I overcomplicated some part of the code?