I'm trying to solve the Spotify bestbefore programming challenge found here http://www.spotify.com/us/jobs/tech/best-before/
I have written the code in Python but when I send it in, it says "Wrong Answer". I have checked my code for bugs many times but unable to find out what is wrong. I know the answers to other's people's code is on the web but I do not want to look at theirs. I'm a firm believer in self-integrity and I really want to solve this myself. I'm just trying to found out some test cases where my code below does not work. Thanks
import sys
def values(year,month,day):
valid = True
#print "Debug: " + str(a) + " " + str(b) + " " + str(c)
#Year
fullYear = year
if year < 2000:
fullYear = year + 2000
#Month
if month > 12:
valid = False
#Day
if month in (4,6,9,11):
if day > 30:
valid = False
elif month == 2:
is_leapyear = detectLeapyear(int(fullYear))
if (is_leapyear):
if day > 29:
valid = False
else:
if day > 28:
valid = False
else:
if day > 31:
valid = False
if not valid:
return 0
else:
return 1
def detectLeapyear(in_year):
rem4 = in_year % 4
if not rem4:
rem100 = in_year % 100
if not rem100:
rem400 = in_year % 400
if not rem400:
return 1
else:
return 0
else:
return 1
else:
return 0
if __name__ == '__main__':
for line in sys.stdin:
#date = sys.argv[1]
date = line.strip()
sArray = date.split('/')
validArray = []
x = int(sArray[0])
y = int(sArray[1])
z = int(sArray[2])
#year-month-day
if values(x,y,z):
validArray.append([x,y,z])
if values(x,z,y):
validArray.append([x,z,y])
if values(y,x,z):
validArray.append([y,x,z])
if values(y,z,x):
validArray.append([y,z,x])
if values(z,x,y):
validArray.append([z,x,y])
if values(z,y,x):
validArray.append([z,y,x])
if validArray:
validArray = sorted(validArray, reverse=False)
#Zero Padding of Numbers
if validArray[0][0] < 2000:
fyear = str(validArray[0][0] + 2000)
if validArray[0][1] < 10:
fmonth = "0" + str(validArray[0][1])
else:
fmonth = str(validArray[0][1])
if validArray[0][2] < 10:
fday = "0" + str(validArray[0][2])
else:
fday = str(validArray[0][2])
#print "=>" + str(validArray[0][0]) + "-" + str(validArray[0][1]) + "-" + str(validArray[0][2])
print fyear + "-" + fmonth + "-" + fday
#print validArray
else:
print date + " is illegal"
These are the Test cases I have and they all seem to work:
12/11/10
02/4/67
31/9/73
31/12/2999
2000/6/12
2000/06/12
2000/12/6
2000/12/06
13/12/67
02/4/67
31/9/73
99/99/99
Program output:
2010-11-12
2067-02-04
31/9/73 is illegal
2067-12-31
2067-06-12
2067-06-12
2067-06-12
2067-06-12
2067-12-13
2067-02-04
31/9/73 is illegal
99/99/99 is illegal