Me pregunto cómo resolver este problema con Python básico ( no se deben usar bibliotecas ): Cómo calcular cuándo será (/ sería) el día 10000 de uno después de su cumpleaños. Por ejemplo, dado el lunes 19/05/2008, el día deseado es el viernes 05/10/2035 (según https://www.durrans.com/projects/calc/10000/index.html?dob=19%2F5%2F2008&e =mc2 )
Lo que he hecho hasta ahora es el siguiente script:
years = range(2000, 2050) lst_days = [] count = 0 tot_days = 0 for year in years: if((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0)): lst_days.append(366) else: lst_days.append(365) while tot_days <= 10000: tot_days = tot_days + lst_days[count] count = count+1 print(count)que estima la edad de la persona después de 10.000 días desde su cumpleaños (para personas nacidas después de 2000). Pero estoy desconcertado de cómo proceder.
Aquí hay una solución que se me ocurrió que no involucra bibliotecas ni paquetes, solo bucles y condicionales (cuentas para años bisiestos):
def isLeapYear(years): if years % 4 == 0: if years % 100 == 0: if years % 400 == 0: return True else: return False else: return True else: return False monthDays = [31,28,31,30,31,30,31,31,30,31,30,31] sum = 0 sumDays = [] for i in monthDays: sumDays.append(365 - sum) sum += i timeInp = input("Please enter your birthdate in the format dd/mm/yyyy\n") timeInp = timeInp.split("/") days = int(timeInp[0]) months = int(timeInp[1]) years = int(timeInp[2]) totDays = 10000 if totDays > 366: if isLeapYear(years): if months == 1 or months == 2: totDays -= (sumDays[months - 1] + 1 - days) + 1 else: totDays -= (sumDays[months - 1] - days) + 1 else: totDays -= (sumDays[months - 1] - days) + 1 months = 1 days = 1 years += 1 while totDays > 366: if isLeapYear(years): totDays -= 366 else: totDays -= 365 years += 1 i = 0 while totDays != 0: if isLeapYear(years): monthDays[1] = 29 else: monthDays[1] = 28 if totDays >= monthDays[i]: months += 1 totDays -= monthDays[i] elif totDays == monthDays[i]: months += 1 totDays = 0 else: days += totDays if days % (monthDays[i] + 1)!= days: days %= monthDays[i] + 1 months += 1 totDays = 0 if months == 13: months = 1 years += 1 i += 1 if i == 12: i = 0 print(str(days) + "/" + str(months) + "/" + str(years)) Como sugiere el nombre, isLeapYear() toma un parámetro years y devuelve un valor booleano.
Nuestro primer paso para este problema, para hacerlo más fácil, es simplemente "traducir" nuestra fecha al próximo año. Esto facilita nuestros cálculos futuros. Para ello, podemos definir un array sumDays que almacene la cantidad de días que tarda cada mes en terminar el año (ir a año nuevo). Luego, restamos esta cantidad de totDays , tomamos en cuenta los años bisiestos y actualizamos nuestras variables.
Lo siguiente es la parte fácil, saltando los años mientras tenemos suficientes días para un año completo.
Una vez que no podemos sumar otro año completo, solo vamos mes a mes hasta que nos quedemos sin días.
¡Espero que esto haya ayudado! Avíseme si necesita más detalles o aclaraciones (o si cometí un error) :)
Ejemplos de casos de prueba:
Entrada #1:
19/05/2008Salida #1:
5/10/2035Entrada #2:
05/05/2020Salida #2:
21/9/2047Entrada #3:
29/02/2020Salida #3:
17/7/2047Revisé la mayoría de mis soluciones con este sitio web:https://www.countcalculate.com/calendar/birthday-in-days/result
Si importa la fecha y hora de la biblioteca
import datetime your_date = "01/05/2000" (day, month, years) = your_date.split("/") date = datetime.date(int(years), int(month), int(day)) date_10000 = date+datetime.timedelta(days=10000) print(date_10000)Sin script de biblioteca
your_date = "20/05/2000" (day, month, year) = your_date.split("/") days = 10000 year = int(year) month = int(month) day = int(day) end=False #m1,m3,m5,m7,m8,m10,m12=31 #m2=28 #m4,m6,m9,m11=30 m=[31,28,31,30,31,30,31,31,30,31,30,31] while end!=True: if(((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0)) and(days-366>=0)): days-=366 year+=1 elif(((year % 400 != 0) or (year % 100 != 0) and (year % 4 != 0)) and(days-366>=0)): days-=365 year+=1 else: end=True end=False if(((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0))): m[1]=29 else: m[1]=28 while end!=True: if(days-m[month]>=0): days-=m[month] if(month+1!=12): month+=1 else: year+=1 if(((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0))): m[1]=29 else: m[1]=28 month=0 else: end=True if(day+days>m[month]): day=day+days-m[month]+1 if(month+1!=12): month+=1 else: year+=1 if(((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0))): m[1]=29 else: m[1]=28 month=0 else: day=day+days print(day,"/",month,"/",year)He actualizado mi código para el año bisiesto y las fechas del mes. Aquí está el código que estoy usando:
n = input("Enter your DOB:(dd/mm/yyyy)") d,m,y = n.split('/') #Splitting the DOB d,m,y = int(d), int(m), int(y) def if_leap(year): # Checking for leap year. if year % 4 != 0: return False elif year % 100 == 0 and year % 400 != 0: return False else: return True target = 10000 while target > 364: # getting no.of years if if_leap(y): target -= 366 y += 1 else: target -= 365 y += 1 while target > 27: # getting no. of months if m == 2 : if if_leap(y): target -= 29 m += 1 if m >= 12: # Resetting the month to 1 if it's value is greater than 12 y += 1 m -= 12 else: target -= 28 m += 1 if m >= 12: y += 1 m -= 12 elif m in [1, 3, 5, 7, 8, 10, 12]: target -= 31 m += 1 if m >= 12: y += 1 m -= 12 elif m in [4, 6, 9, 11]: target -= 30 m += 1 if m >= 12: y += 1 m -= 12 d = d + target # getting the no. of days if d > 27: if m == 2: if if_leap(y): d -= 29 m += 1 else: d -= 28 m += 1 elif m in [1, 3, 5, 7, 8, 10, 12]: d -= 31 m += 1 else: d -= 30 m += 1 print(f"The 10000th date will be {d}/{m}/{y}")Producción:
Enter your DOB:(dd/mm/yyyy): 06/01/2006 The 10000th date will be 24/5/2033 PD: Obtengo algunos resultados ligeramente diferentes cuando consulto con ese sitio web. ¿Alguien puede averiguar el error / error en el código? Será muy útil. Por ej. para la fecha 08/12/2004 , debería ser 25/4/2032 , pero mi salida muestra 24/4/2032 .