Created
December 6, 2019 13:21
School opdracht niet kwijt raken please
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DAYS_IN_MONTH = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) | |
MONTH_NUMBER = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5] | |
YEAR_NUMBER = [0,1,2,3,5,6,0,1,3,4,5,6,1,2,3,4,6,0,1,2,4,5,6,0,2,3,4,5] | |
CENTURY_NUMBER = [6, 4, 2, 0] | |
DAYS = ["zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"] | |
def problem(): | |
print("Deze datum is ongeldig.") | |
def yearnumber(year): | |
y = year | |
while y > 27: | |
y = y - 28 | |
return YEAR_NUMBER[y] | |
def centurynumber(century): | |
c = century | |
while c > 3: | |
c = c - 4 | |
# 15 > 3, 16 > 0, 17 > 1, 18 > 2 | |
return CENTURY_NUMBER[c] | |
def date(): | |
date_str = input("Voer een datum in (dd-mm-YYYY): ") | |
parts = date_str.split('-') | |
if len(parts) != 3: | |
problem() | |
else: | |
for i in range(len(parts)): | |
parts[i] = parts[i].strip() | |
if len(parts[i]) == 0 or not parts[i].isdigit(): | |
problem() | |
return int(parts[0]), int(parts[1]), int(parts[2]) | |
def valid_date(day, month, year): | |
if month < 1 or month > 12 or day < 1 or year < 0: | |
return False | |
days_in_month = DAYS_IN_MONTH[month - 1] | |
if month != 2: | |
return day <= days_in_month | |
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0: | |
days_in_month += 1 | |
return day <= days_in_month | |
def main(): | |
given_date = date() | |
if valid_date(given_date[0], given_date[1], given_date[2]): | |
# Datum is geldig | |
year = (given_date[2] % 100) % 7 | |
century = int((given_date[2] - year) / 100) + 1 | |
print(DAYS[2 + (given_date[0] + MONTH_NUMBER[(given_date[1] - 1)] + yearnumber(year) + centurynumber(century)) % 7]) | |
else: | |
# Datum is ongeldig! | |
problem() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment