Ver Mensaje Individual
  #8 (permalink)  
Antiguo 02/02/2014, 14:01
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 9 meses
Puntos: 1360
Respuesta: Desafió 2014 - Semana 2

Mi versión.

Código Python:
Ver original
  1. from datetime import date
  2. from datetime import timedelta
  3.  
  4.  
  5. def next_day(some_date, day):
  6.     "day: 0 - monday ... 6 - sunday"
  7.     offset = ((7 + day) - some_date.weekday()) % 7
  8.     return some_date + timedelta(days=offset)
  9.  
  10. def count_days(from_date, to_date, day):
  11.     new_from_date = next_day(from_date, day)
  12.     if new_from_date > to_date:
  13.         return 0
  14.     else:
  15.         return ((to_date - new_from_date).days / 7 + 1)
  16.  
  17. def weekdays(from_date, to_date):
  18.     """
  19.        Count the days of rest
  20.    """
  21.     sats = count_days(from_date, to_date, 5)
  22.     suns = count_days(from_date, to_date, 6)
  23.     return sats + suns
  24.  
  25. if __name__ == '__main__':
  26.     from time import time
  27.     t1 = time()
  28.     assert weekdays(date(2013, 9, 18), date(2013, 9, 23)) == 2, "Error en test 1"
  29.     assert weekdays(date(2013, 1, 1), date(2013, 2, 1)) == 8, "Error en test 2"
  30.     assert weekdays(date(2013, 2, 2), date(2013, 2, 3)) == 2, "Error en test 3"
  31.     assert weekdays(date(1999, 1, 1), date(2000, 1, 1)) == 105, "Error en test 4"
  32.     assert weekdays(date(2004, 2, 1), date(2004, 2, 29)) == 9, "Error en test 5"
  33.     assert weekdays(date(1980, 8, 8), date(2012, 3, 3)) == 3295, "Error en test 6"
  34.     assert weekdays(date(2001, 2, 2), date(2007, 12, 31)) == 722, "Error en test 7"
  35.     assert weekdays(date(2002, 9, 9), date(2002, 9, 10)) == 0, "Error en test 8"
  36.     assert weekdays(date(2011, 1, 1), date(2012, 2, 1)) == 114, "Error en test 9"
  37.     assert weekdays(date(1995, 3, 2), date(1999, 2, 3)) == 410, "Error en test 10"
  38.     assert weekdays(date(1992, 2, 1), date(2000, 1, 1)) == 827, "Error en test 11"
  39.     assert weekdays(date(1998, 5, 1), date(2001, 2, 22)) == 294, "Error en test 12"
  40.     assert weekdays(date(2003, 8, 2), date(2005, 3, 3)) == 166, "Error en test 13"
  41.     assert weekdays(date(2010, 2, 5), date(2010, 12, 31)) == 94, "Error en test 14"
  42.     t2 = time()