Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Last active October 22, 2019 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snawoot/1645391c52fb2fbae76dbd89653877b9 to your computer and use it in GitHub Desktop.
Save Snawoot/1645391c52fb2fbae76dbd89653877b9 to your computer and use it in GitHub Desktop.
Dates which can be confused in different date format (DD/MM and MM/DD) even if week day is specified
#!/usr/bin/env python3
month_names = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
months = [
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
]
months_leap = [
31,
29,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
]
def build_date_to_num(months):
return dict(
((mon, day), num) for num, (mon, day) in enumerate(
(mon, day) for mon, count in enumerate(months) for day in range(count)
)
)
date_to_num = build_date_to_num(months)
date_to_num_leap = build_date_to_num(months_leap)
def week_collisions(months, date_to_num):
for mon, count in enumerate(months):
for day in range(mon):
my_num = date_to_num[(mon, day)]
other_num = date_to_num[(day, mon)]
if (my_num - other_num) % 7 == 0:
yield mon, day
def format_pair(mon, day):
return "\t%s %d <=> %s %d" % (month_names[day], mon + 1, month_names[mon], day + 1)
def main():
print("non-leap year:")
for mon, day in week_collisions(months, date_to_num):
print(format_pair(mon, day))
print("leap year:")
for mon, day in week_collisions(months_leap, date_to_num_leap):
print(format_pair(mon, day))
if __name__ == '__main__':
main()
non-leap year:
January 7 <=> July 1
February 8 <=> August 2
May 9 <=> September 5
January 11 <=> November 1
July 11 <=> November 7
March 12 <=> December 3
leap year:
February 3 <=> March 2
January 6 <=> June 1
May 9 <=> September 5
July 11 <=> November 7
February 12 <=> December 2
March 12 <=> December 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment