Respuesta: Desafió 2014 - Semana 2 Bueno, hecho de dos formas:
Código Python:
Ver originalfrom datetime import date def weekdays(from_date, to_date): correccion = 0 diferencia = (to_date - from_date).days + 1 dia_inicio = date.weekday(from_date) diferencia_corregida = diferencia + dia_inicio if dia_inicio == 6 : correccion += -1 if diferencia_corregida % 7 == 6: correccion += 1 return diferencia_corregida / 7 * 2 + correccion if __name__ == '__main__': from time import time t1 = time() assert weekdays(date(2013, 9, 18), date(2013, 9, 23)) == 2, "Error en test 1" assert weekdays(date(2013, 1, 1), date(2013, 2, 1)) == 8, "Error en test 2" assert weekdays(date(2013, 2, 2), date(2013, 2, 3)) == 2, "Error en test 3" assert weekdays(date(1999, 1, 1), date(2000, 1, 1)) == 105, "Error en test 4" assert weekdays(date(2004, 2, 1), date(2004, 2, 29)) == 9, "Error en test 5" assert weekdays(date(1980, 8, 8), date(2012, 3, 3)) == 3295, "Error en test 6" assert weekdays(date(2001, 2, 2), date(2007, 12, 31)) == 722, "Error en test 7" assert weekdays(date(2002, 9, 9), date(2002, 9, 10)) == 0, "Error en test 8" assert weekdays(date(2011, 1, 1), date(2012, 2, 1)) == 114, "Error en test 9" assert weekdays(date(1995, 3, 2), date(1999, 2, 3)) == 410, "Error en test 10" assert weekdays(date(1992, 2, 1), date(2000, 1, 1)) == 827, "Error en test 11" assert weekdays(date(1998, 5, 1), date(2001, 2, 22)) == 294, "Error en test 12" assert weekdays(date(2003, 8, 2), date(2005, 3, 3)) == 166, "Error en test 13" assert weekdays(date(2010, 2, 5), date(2010, 12, 31)) == 94, "Error en test 14" t2 = time() print("Tiempo de ejecucion %0.3fs" % (t2 - t1)) print("Todos los tests completados")
Y.
Código Python:
Ver originalfrom datetime import date, timedelta def weekdays(from_date, to_date): diferencia = (to_date - from_date).days +1 count = 0 for i in xrange(diferencia): if date.weekday(from_date) > 4: count += 1 from_date += timedelta(days=1) return count if __name__ == '__main__': from time import time t1 = time() assert weekdays(date(2013, 9, 18), date(2013, 9, 23)) == 2, "Error en test 1" assert weekdays(date(2013, 1, 1), date(2013, 2, 1)) == 8, "Error en test 2" assert weekdays(date(2013, 2, 2), date(2013, 2, 3)) == 2, "Error en test 3" assert weekdays(date(1999, 1, 1), date(2000, 1, 1)) == 105, "Error en test 4" assert weekdays(date(2004, 2, 1), date(2004, 2, 29)) == 9, "Error en test 5" assert weekdays(date(1980, 8, 8), date(2012, 3, 3)) == 3295, "Error en test 6" assert weekdays(date(2001, 2, 2), date(2007, 12, 31)) == 722, "Error en test 7" assert weekdays(date(2002, 9, 9), date(2002, 9, 10)) == 0, "Error en test 8" assert weekdays(date(2011, 1, 1), date(2012, 2, 1)) == 114, "Error en test 9" assert weekdays(date(1995, 3, 2), date(1999, 2, 3)) == 410, "Error en test 10" assert weekdays(date(1992, 2, 1), date(2000, 1, 1)) == 827, "Error en test 11" assert weekdays(date(1998, 5, 1), date(2001, 2, 22)) == 294, "Error en test 12" assert weekdays(date(2003, 8, 2), date(2005, 3, 3)) == 166, "Error en test 13" assert weekdays(date(2010, 2, 5), date(2010, 12, 31)) == 94, "Error en test 14" t2 = time() print("Tiempo de ejecucion %0.3fs" % (t2 - t1)) print("Todos los tests completados")
La primera es más rápida, la segunda la encuentro mas lógica.
Saludos. |