How to get a day based on a number from 1 to 365

Question:

I want to get a day based on a number from 1 to 365. An integer in the range of 1 to 365 is given and I need to find the day of the week for a given day in a year (starting with Sunday).

a = int(input())  # Integer from 1 to 365
print( # day )

Example: input 1, output 4

Asked By: Alice

||

Answers:

I’m not going to give you the code.
The trick here is use something called a modulo.
It’s like a remainder. Eg, 10 modulo 7 -> 7 goes into 10 only one time and there is a remainder of 3, so 10 modulo 7 = 3. The symbol for modulo is a percentage sign %

So every time you get to the 8th day, you want to start back at the beginning. Eg:

0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
6 -> 6
7 -> 0 <--- 7 % 7 = 0, eg 7 goes into 7 only once and with no remainders
8 -> 1
9 -> 2
etc
Answered By: dangarfield

Here is the code:

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
day_number = int(input("What day do you want to get: "))
day = days[day_number % 7]
print(f"Your day: {day}")
Answered By: BokiX

Here’s what they want you to solve:
It is known that the first day of the year is a Thursday, if they were to give you another day of the year, you have to find what day of the week it is.

For example, if they ask you what day of the week it is on day 2 (of the year), then you would say it is a Friday (since day 1 is a Thursday).

But instead of answering "Friday", you would output the number 5, since that number corresponds to Friday.

0 – Sunday, 1 – Monday, 2 – Tuesday, 3 – Wednesday, 4 – Thursday , 5 – Friday, 6 – Saturday

The solution only requires some simple math – see if you can figure it out.

Answered By: Static Spaghetti 13

The question i suppose is asking for the index assigned to the days given initially respective to the Kth day given.

A simple direct algorithm here would be to note that a day gets repeated after 7 days,i.e, 1 week. So finding the modulo of K-1 with 7 would give you the offset with respective to the first day that is a thursday. Adding that to that day would give you the day which would represent the Kth day and return the appropriate index according to that.

Answered By: Rameez Naufal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.