2
\$\begingroup\$

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
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot this has fixed my problem and it was accepted as an answer. This has helped me learn python better, thanks again. \$\endgroup\$
    – Ray Y
    Jan 30, 2012 at 6:34
0
\$\begingroup\$

I'm not sure if you can use the sys.stdin. I know it works with data = raw_input(), so you should probably use that.

Also, in you programs inputs and outputs, there's some wrong dates. For some reason the output is 2067 even though you inputed 2000. I cannot see why this should be like that out from your code.

Also, are you sure

validArray = sorted(validArray, reverse=False)

sorts the code properly? you know you should have the date on form [x < y < z]

Other than that, I can't see why your code isn't working.

Just a couple of things:

  1. Why do you use

    if not valid:
        return 0
    else:
        return 1
    

    instead of

    if valid:
        return 1
    else:
        return 0
    

    ?

  2. I don't like this too much either:

    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
    

    You should try this instead:

    if in_year % 4 == 0:
        if in_year % 100 == 0:
            if in_year % 400 == 0:
                return 1
            else:
                return 0
        else:
            return 1
    else:
       return 0:
    
  3. In the __main__ function, you shoud probable use a for function:

    for year, month, date in ...
    

    To do this, you probably have to rewrite your code a little bit.

If you want to, you can look at my solution and the answers, that were really good. I see you can use some of the same tips as i got. Spotify Best Before Puzzle

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.