Day of the week P18777


Statement
 

pdf   zip   main.cc   main.c   main.java   main.py   main.hs

Write a function that, given a valid date made with a day @d@, a month @m@ and a year @y@, returns its day of the week, that is, “Monday”, or “Tuesday”, or …

To compute it, use the congruence of Zeller. Let dd be the day, mm be the month, and yy be the year. Then,

  1. Subtract two from the month mm, and if the result is zero or less, add 12 to the month and subtract one from the year. Call mm' the new month and call yy' the new year.

  2. Compute the century cc (the first two digits of the year) from the year yy'.

  3. Compute the year aa inside the century (the two last digits of the year) from the year yy'.

  4. Compute f=2.6m0.2+d+a+a/4+c/42c.f = \lfloor 2.6m' - 0.2 \rfloor + d + a + \lfloor a/4 \rfloor + \lfloor c/4 \rfloor - 2c.

  5. Finally, ff modulo 7 gives us the desired result, taking into account that 0 represents Sunday, 1 represents Monday, 2 represents Tuesday, …, and 6 represents Saturday.

Interface

C++
string day_of_the_week (int d, int m, int y);
C++
char* day_of_the_week (int d, int m, int y);
Java
public static String day_of_the_week (int d, int m, int y);
Python
day_of_the_week (d, m, y)  # returns str
day_of_the_week (d: int, m: int, y: int) -> str
Haskell
dayOfTheWeek :: Int -> Int -> Int -> String

Precondition

The parameter @y@ is between 1800 and 9999, both included. The date is valid.

Hint

Watch out for modulos of negative numbers!

Observation

You only need to submit the required procedure; your main program will be ignored.

Public test cases
  • Input/Output

    day_of_the_week(28, 2, 2000) → Monday
    day_of_the_week(29, 2, 2000) → Tuesday
    day_of_the_week(1, 3, 2000) → Wednesday
  • Information
    Author
    Jordi Petit
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C C++ Haskell Java Python
    User solutions
    C C++ Java Python