Identify the day of the week of a given date

Asked

Viewed 3,007 times

0

One of the questions of a college task is giving work to the whole class, so far no one has been able to answer question 4. A friend of mine tried with this code, but it doesn’t work right, because depending on the entries, the month comes out 13, 14, 19 and countless other non-existent months. Could someone give me a hand? PS: Needs to be in python 3.

Question: You will have a wonderful holiday that starts on Wednesday 3rd. You will return from your holiday after 137 nights (Wow!). Write a program that asks for the day of the month and the day of the week that you will travel and ask for the number of days that you will be on vacation and print the day of the week that you will return.

Code:

def questao4(m,s,f):
    f = f+m
    t = f%30
    rs = t%7
    return print("Mês =", t,"Dia da semana =", rs)
questao4(9,3,1)
  • I understand that the question is about calculus, probably to teach something, but I must say that considering possibly time zones, variations of days for each month and something more perhaps, I humbly suppose that it will not be accurate to do only on the basis of the calculation, would have to use some lib that works with dates and Timezone.

  • Assuming I was doing based on the Timezone of Brazil, I would be able to give some hint of how to do this?

  • Timezone Brazil or does not vary as or may vary, see even the president could have taken the summer time of SP, but did not, maintained, which would already affect the application, a lib so will work based on the time of the machine and the machine (be it Windows, or Linux) lower the updated time and thus you would achieve "accuracy" (approximately, with loss of seconds maybe, or if the user changes something on the machine, as the time). What I mean is that if the task of the college is intended to teach mathematics + programming to lib and time zones will not solve anything, because ...

  • ... the focus of the task question is to learn mathematics with python, now if that is not the goal of your teacher then yes a Python LIB that works with Timezone and etc would be the ideal way, I can only give you "tips" or even make a suggestion if you are sure that your teacher has explained the intention of this.

  • The teacher does not prioritize that we learn mathematics, but rather that we are able to encode what is requested.

  • I understand John, but many exercises may have some basis to be applied so anyway I hope that then the use of libs date and timedelta work it out for you.

  • So just to finish, I I suppose that informing the day of the week in the question is a huge indication that the problem is of mathematics, because if it is not then inform the day of the week would be irrelevant in a use with date libs/Apis, just the month and day of it that the rest of the to solve in the "API" same.

  • The "worst" is that it’s not really about focusing on mathematics, it’s about solving what is asked doesn’t matter whether you use math at all, or just thinking. I appreciate the help x)

Show 3 more comments

2 answers

3

This issue can be solved easily with the library datetime, creating an object date representing the date and a timedelta representing the time interval to be considered.

from datetime import date, timedelta

DIAS = [
    'Segunda-feira',
    'Terça-feira',
    'Quarta-feira',
    'Quinta-Feira',
    'Sexta-feira',
    'Sábado',
    'Domingo'
]

inicio = date(year=2018, month=4, day=26)
ferias = timedelta(days=137)
final = inicio + ferias

print(DIAS[final.weekday()])  # Segunda-feira

See working on Repl.it | Ideone | Github GIST

But since the focus of the exercise is probably mathematics, just think about the operations to be done. If I go on vacation on the 3rd, Wednesday, and I stay on vacation for 137 nights, that means I get 19 full weeks plus four days. At the end of 19 weeks, I will know that it will again be a Wednesday, so I just check which day of the week will be 4 days after (but as it is a college exercise, I leave the implementation of this logic with you).

  • Very good, I only fear that the exercise/task is intended to teach math+programming, as I explained better in the comments above, anyway the code is very good and will help a lot of people, I hope this is the way to AP also +1

  • The intention is not even toward mathematics, but rather to ensure that we can encode what is being asked for no matter what. I really appreciate the help x)

0

x = input("Quantos dias voce ficara de ferias? ")
duracao_ferias = int(x)


y = input("Digite o dia do mes que voce ira viajar: ")
dia_viagem = int(y)

dia_retorno = duracao_ferias + dia_viagem
if (dia_retorno > 30):
    dia_retorno = (duracao_ferias + dia_viagem) % 30

z = input("Digite o dia da semana que voce ira viajar: ")

if (z == "Domingo"): 
    z = 0
if (z == "Segunda-feira"): 
    z = 1
if (z == "Terca-feira"): 
    z = 2    
if (z == "Quarta-feira"): 
    z = 3    
if (z == "Quinta-feira"): 
    z = 4    
if (z == "Sexta-feira"): 
    z = 5    
if (z == "Sabado"): 
    z = 6

dia_semana_retorno = duracao_ferias % 7 + z
if (dia_semana_retorno > 6):
    dia_semana_retorno = (duracao_ferias % 7 + z) - 7

if (dia_semana_retorno == 0):
    dia_semana_retorno = "Domingo"
if (dia_semana_retorno == 1):
    dia_semana_retorno = "Segunda-feira"
if (dia_semana_retorno == 2):
    dia_semana_retorno = "Terca-feira"
if (dia_semana_retorno == 3):
    dia_semana_retorno = "Quarta-feira"
if (dia_semana_retorno == 4):
    dia_semana_retorno = "Quinta-feira"
if (dia_semana_retorno == 5):
    dia_semana_retorno = "Sexta-feira"
if (dia_semana_retorno == 6):
    dia_semana_retorno = "Sabado"

print("Voce voltara no dia",dia_retorno,dia_semana_retorno)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.