Spotify Bestbefore Coding Challenge in Python 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 &lt; 2000:
        fullYear = year + 2000

    #Month
    if month &gt; 12:
        valid = False

    #Day    
    if month in (4,6,9,11):
        if day &gt; 30:
            valid = False
    elif month == 2:
        is_leapyear = detectLeapyear(int(fullYear))
        if (is_leapyear):
            if day &gt; 29:
                valid = False
        else:
            if day &gt; 28:
                valid = False
    else:
        if day &gt; 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] &lt; 2000:
                fyear = str(validArray[0][0] + 2000)

            if validArray[0][1] &lt; 10:
                fmonth = "0" + str(validArray[0][1])
            else:
                fmonth = str(validArray[0][1])

            if validArray[0][2] &lt; 10:
                fday = "0" + str(validArray[0][2])
            else:
                fday = str(validArray[0][2])
            #print "=&gt;" + 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
I just went over the last version of your code you posted on freenode's #python, and there are two major errors in it:

You never check if month or day are 0, and your month > 12 check gets overwritten by the later if clauses. With those fixed, it passes.

Same issues are already in the code above, but I did not check if it has others as well.

the flag variable approach is very error prone and hard that way - it would be better to return False right away if you determine that, not tag it along with the chance that it might get flipped back erroneously.