Search code examples
convertersdigitsweekday

Convert 6 digit number into weekday (python)


I am currently trying to convert a 6 digit number into a day of the week. For example, I want "150102" to be converted as the weekday Friday "15" is the year 2015", "01" is the month, and "02" is the day

Note I want to do this without importing any time functions and using the language python.


Solution

  • Well, judging by your comments, this is an assignment (which means it should be marked as such). So I'm afraid my solution will be vague, but provide some guidance on a (read, probably not the best) solution.

    First, you want to have something which splits the given String into the year/month/date.

    public class Date {
        private int year, month, date;
    
        public Date(final String given) {
            // TODO - Extract year / month / date.
            // Validate year is supported, month exists and date
            // exists in month...
        }
    }
    

    Next, your gonna (probably) need some fixed point which you know the weekday of (say, Jan. 1, 2000; saturday).

    Then use your knowledge of days in year/leap-year, each month, etc. to find how many days since that date the provided argument was.

    static int daysSinceKnown(Date date) {
        // TODO - How many years since, how many of those were leap-years, etc.
    }
    

    Finally, modular arithmetic can give you the weekday from that function's return. (Specifically, looking at that value mod 7 (%7) would give you a number 0-6 [negative numbers won't work nicely, but you can catch that when validating the year] which you can interpret as a day of the week.)