Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

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.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post almost 3 years ago by Alexei‭.

29 / 255
  • 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?

Suggested almost 3 years ago by hkotsubo‭