r/Python 2h ago

Resource Date calculator (en)

Upvotes

import datetime

def calculate_weekday(day, month, year):

"""

Calculates the day of the week for any date using the Doomsday Algorithm.

Returns a string with the day name in English.

"""

days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

# 1. Check if the year is a leap year according to Gregorian rules

is_leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

# 2. Identify the century anchor day (400-year cycle)

century_base = (year // 100) * 100

if century_base % 400 == 0:

century_anchor = 2 # Tuesday (e.g., years 2000, 1600)

elif century_base % 400 == 100:

century_anchor = 0 # Sunday (e.g., years 2100, 1700)

elif century_base % 400 == 200:

century_anchor = 5 # Friday (e.g., years 2200, 1800)

else:

century_anchor = 3 # Wednesday (e.g., years 1900, 2300)

# 3. Calculate the year component (Standard Doomsday Algorithm)

year_last_digits = year % 100

quotient_4 = year_last_digits // 4

year_doomsday = (century_anchor + year_last_digits + quotient_4) % 7

# 4. Map the monthly anchor dates

month_anchors = {

1: 4 if is_leap_year else 3, # January 4 (leap year) / January 3 (normal year)

2: 29 if is_leap_year else 28, # February 29 (leap year) / February 28 (normal year)

3: 14,

4: 4,

5: 9,

6: 6,

7: 11,

8: 8,

9: 5,

10: 10,

11: 7,

12: 12

}

# 5. Calculate the distance from the closest monthly anchor

current_anchor = month_anchors[month]

distance = day - current_anchor

# 6. Resolve the final day index (modulo 7)

final_day_index = (year_doomsday + distance) % 7

return days_of_week[final_day_index]

def validate_date(day, month, year):

"""Checks if a combination of day, month, and year is a valid real date."""

try:

datetime.date(year, month, day)

return True

except ValueError:

return False

if __name__ == "__main__":

print("=== USER INTERFACE ===")

print("Enter the dates you want to calculate. Type 'exit' to quit.\n")

while True:

try:

date_input = input("Enter date (DD/MM/YYYY): ").strip()

if date_input.lower() == 'exit':

print("Closing the program.")

break

# Parsing the user input

parts = date_input.split('/')

if len(parts) != 3:

raise ValueError

d = int(parts[0])

m = int(parts[1])

y = int(parts[2])

# Calendar validation

if not validate_date(d, m, y):

print("Error: The entered date does not exist (e.g., check leap years or month limits). Try again.\n")

continue

# Final calculation and output

result_day = calculate_weekday(d, m, y)

print(f"The date {date_input} corresponds to: {result_day}\n")

except ValueError:

print("Error: Invalid format. Please use exactly the DD/MM/YYYY format (e.g., 23/02/2016).\n")


r/Python 12h ago

Discussion DAE feel like python is a lot harder now

Upvotes

when i started python 10 years ago it felt like a baby language compared to C++ and C. now it feels as just as hard as those two . its because of the additional library knowledge u need to know for ai ml etc


r/Python 2h ago

Discussion amazon ml challenge 2-26 (ml require python)

Upvotes

need 3 members for the team .

about me : am 2027 final year undergrad , have decent knowledge of ML and DL and wont claim myself as too muh of an expert . Has practical experince of such comepetitions .

what i am looking for :

dedicated team members who can work for 72 hour hackathon wthout ghosting at end time or not doing anything , you can 2027 or 2028 grad but should jave decent enough knowledge and practical experince too . i am not doing this for timepass but aimig for top 50 to get ppi if everything go well .

only serious people message , no ghosters !


r/Python 6h ago

Discussion Why can’t python devs just move to c++ or rust or smth

Upvotes

I feel like ai is too good at python. I’m worried about Python devs and their future because I feel like I can 1 shot projects with a prompt on Claude. I think it would be cool if all the Python devs maybe start changing their projects to other stuff just so ai doesn’t get too powerful ykwim?


r/Python 9h ago

Daily Thread Tuesday Daily Thread: Advanced questions

Upvotes

Weekly Wednesday Thread: Advanced Questions 🐍

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

How it Works:

  1. Ask Away: Post your advanced Python questions here.
  2. Expert Insights: Get answers from experienced developers.
  3. Resource Pool: Share or discover tutorials, articles, and tips.

Guidelines:

  • This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
  • Questions that are not advanced may be removed and redirected to the appropriate thread.

Recommended Resources:

Example Questions:

  1. How can you implement a custom memory allocator in Python?
  2. What are the best practices for optimizing Cython code for heavy numerical computations?
  3. How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
  4. Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
  5. How would you go about implementing a distributed task queue using Celery and RabbitMQ?
  6. What are some advanced use-cases for Python's decorators?
  7. How can you achieve real-time data streaming in Python with WebSockets?
  8. What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
  9. Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
  10. What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)

Let's deepen our Python knowledge together. Happy coding! 🌟