Spotify puzzle problem

后端 未结 2 1398
夕颜
夕颜 2021-01-13 11:17

I\'m trying to solve the \"best before\" Spotify puzzle described on this page. Basically, with an input of three integers separated by slashes (e.g. 11/3/4) you\'re suppose

2条回答
  •  暖寄归人
    2021-01-13 12:00

    One bug: it accepts 2100/02/29. 2100 is not a leap year, so there is no 2011/02/29.

    If I were you, I would use SimpleDateFormat for parsing and validating (hint: lenient parsing). It's much simpler, much intuitive and the code would be easier to read. (Don't Reinvent The Wheel)

    Some other thoughts above.

    Unnecessary assignments: return dateOK = false;

    Just return with false:

    return false;
    

    (The dataOK variable is unnecessary in your case.)

    public static void illegal(String dateInput){
        System.out.println(dateInput + " is illegal");
        System.exit(0); 
    }
    

    Throw exceptions instead of System.exit().

    In the getBestDate() method the last two lines never run. They are dead code (since illegal() calls System.exit()):

    }else{
        illegal(dateInput);
    }
    
    Integer[] bestDate = {var1, var2, var3};
    return bestDate;
    

    If it's possible avoid static methods and fields. Lastly, it's a good problem to learn how to write unit tests.

提交回复
热议问题