code challenges - language of your choice

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Hi Folks,

Here's an idea for a thread. Someone posts a challenge (that they've already completed) with the expected results and a deadline to solve the problem.

If it's solved before the deadline feel free to post screenshots of the result. but not the code itself. You may use any language you like, I used python for this

I'm happy for the idea to evolve with further rules etc. But let's keep it simple for the moment

The challenge that follows the last can be something completely new or one that builds on the challenge that was just solved

I'll go first

Assume you have an object that has a from date and to date.

Now create a number of these objects

A-> fromDate 27/9/13, toDate 1/10/13
B-> fromDate 5/10/13, toDate 10/10/13
C-> fromDate 2/10/13, toDate 4/10/13
D-> fromDate 13/10/13, toDate 15/10/13

The idea is to find any missing date coverages, take the lists below for example

list1 = [A, B, C, D]
list2 = [A,B,D]
list3 = [A,B,C]
list4= [A,B]

Feeding the above lists into a method will give the results below

list1: "missing coverage for 11/10/13->12/10/13"
list2: "missing coverage for 2/10/13->4/10/13 + 11/10/13->12/10/13"
list3: "no missing coverage"
list4: "missing coverage for 2/10/13->4/10/13

So deadline 29/9/13 @ 8pm? And just to prove it's not my homework

sw7v.jpg


DEADLINE finished

Here's mine, could've made it shorter by creating the MyDate objects/lists all in one line but decided to keep it clearer :)

Code:
from datetime import date, timedelta

class MyDate:
   def __init__(self, _fromDate, _toDate):
      self.fromDate = _fromDate
      self.toDate = _toDate
   
   def __eq__(self, other):
      return (self.fromDate == other.fromDate and self.toDate == other.toDate)
   
   def getFromDate(self):
      return self.fromDate
   
   def getToDate(self):
      return self.toDate

def check_coverage_list(datelist):
  print ("\r\nNew test")
  print ("Input dates")
  print ("######################")
  for mydate in datelist:
     print ("From date: " + str(mydate.getFromDate()))
     print ("To date: " + str(mydate.getToDate())) 
  coverageList = []
  toDatePlusOne = datelist[0].getToDate() + timedelta(days=1)
  for date in datelist[1:]:
    if( date.getFromDate() > toDatePlusOne):
      missingFrom = toDatePlusOne
      missingTo = date.getFromDate() - timedelta(days=1)
      missingDate = MyDate(missingFrom, missingTo)
      if missingDate not in datelist:
        coverageList.append(missingDate)
      toDatePlusOne = date.getToDate() + timedelta(days=1)
  
  print ("\r\nResult")
  print ("######################")
  if len(coverageList) == 0:
     print( "woohoo" )	
  else:
    print( "warning missing coverage for:")
    for date in coverageList:      
      print( "From " + str(date.getFromDate()))
      print( "To " + str(date.getToDate()))    

fromX = date(2013, 9, 27)
toX = date(2013, 10, 1)
fromY = date(2013, 10, 5)
toY = date(2013, 10, 10)
fromZ = date(2013, 10, 2)
toZ = date(2013, 10, 4)
fromA = date(2013, 10 , 13)
toA = date(2013, 10 , 17)

dateA = MyDate(fromA, toA)
dateX = MyDate(fromX, toX)
dateY = MyDate(fromY, toY)
dateZ = MyDate(fromZ, toZ)

#Missing 11/10/13->12/10/13 - working
check_coverage_list([dateX, dateY, dateZ, dateA])

#Missing  2/10/13->4/10/13 + 11/10/13->12/10/13 - working
check_coverage_list([dateX, dateY, dateA])

#Complete date range - working
check_coverage_list([dateX, dateY, dateZ])

#Missing 2/10/13->4/10/13 - working
check_coverage_list([dateX, dateY])
 
Last edited:
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
As I said if you finish before the deadline post a screenshot but not the code itself, then when the deadline is past edit your post to include the code, there are no winners but it's always interesting to see how others have tackled a problem
 
Back
Top Bottom