The worst problem i have ever met in this oj


  • 292
    A

    The description do not give a clear explantion of the definition of a valid Number, we just use more and more trick to get the right solution. It's too bad, it's waste of my time


  • 2
    G

    I use python, and feel that I cheat the problem back when I just use try/except with default casting float(s).
    (yes, it gets accepted just like that. I thought the test data will derp on me again for my amusement.)
    But that is how I usually do when checking for proper numerical input from stdin/ui textfield.

    I agree though, this is pretty much useless question unless the spec is clear.
    Somehow I feel the questions are gear toward jerking around with wrong data input and see if you catch them(and the real problem itself is not that hard), but in reality it is the caller to check the validity so when a function is called millions of times, we don't check input millions of times thus inefficient code.


  • 11
    C

    The question is very popular in tech interviews. I know at least two people got asked for the exactly same question in their LinkedIn interviews. As it states, you need to ask the interviewer what the definition is. I think it can be good preparation for real interviews.


  • 58
    S

    +1 for the worst problem on OJ. This is definitely a valid interview question, but it requires heavy clarification through interaction. In a real interview, you have one and only one source to get all the missing information, a.k.a. your interviewer. On OJ, however, there is no interaction whatsoever except through trial and error. This, as far as I am concerned, is directly against the principle of 'collecting all the info before implementing'. Internet searching also does not work since the word 'numeric' is not even defined in the question. No wonder this relatively easy (implementation-wise) problem has one of the lowest rates of acceptance.

    I would suggest OJ:

    1. provide all the details to let users focus on the implementation, or
    2. leave room for ambiguity but also suggest where the info can be collected, or
    3. provide an interactive applet or something like that on the page so that the user can try the string combinations that they are uncertain of before implementing. I think this is an easy thing to do, and it saves everybody's time.

  • 5
    B

    I use Java's Double.parse to get the answer and it is accepted....very shocking!!!is it supposed to be this simple


  • 0
    Z
    This post is deleted!

  • 0
    S

    totally agreed. some case like ".5" "-.87" etc , are regards as numberic. It`s very weird.


  • 0
    W
    This post is deleted!

  • 2
    M

    I have to disagree because .5 and -.87 are perfectly valid numbers in many languages like C/C++, Java, and Python.


  • 3
    M

    It is true that you don't have access to the interviewer but you can run test cases.


  • 52
    Y

    Cannot agree more and I feel I have a totally different opinion on what can be called a number(cry)

    I will share out the test cases I have in hope to help you understand what OJ define as a "number".

        test(1, "123", true);
        test(2, " 123 ", true);
        test(3, "0", true);
        test(4, "0123", true);  //Cannot agree
        test(5, "00", true);  //Cannot agree
        test(6, "-10", true);
        test(7, "-0", true);
        test(8, "123.5", true);
        test(9, "123.000000", true);
        test(10, "-500.777", true);
        test(11, "0.0000001", true);
        test(12, "0.00000", true);
        test(13, "0.", true);  //Cannot be more disagree!!!
        test(14, "00.5", true);  //Strongly cannot agree
        test(15, "123e1", true);
        test(16, "1.23e10", true);
        test(17, "0.5e-10", true);
        test(18, "1.0e4.5", false);
        test(19, "0.5e04", true);
        test(20, "12 3", false);
        test(21, "1a3", false);
        test(22, "", false);
        test(23, "     ", false);
        test(24, null, false);
        test(25, ".1", true); //Ok, if you say so
        test(26, ".", false);
        test(27, "2e0", true);  //Really?!
        test(28, "+.8", true);  
        test(29, " 005047e+6", true);  //Damn = =|||
    

    Here is the final Regex I got based on their definition

    Pattern.matches("(\\+|-)?(\\d+(\\.\\d*)?|\\.\\d+)(e(\\+|-)?\\d+)?", s);
    

    But I thought my original one should be more rigorous!

    Pattern.matches("-?(([1-9]{1}+\\d*|0)(\\.\\d+)?|\\.\\d+)(e-?[1-9]{1}+\\d*)?", s);

  • 4
    G

    I am also confused about why "abc" is not a number, it could be hexadecimal.


  • 0
    G

    "0." is a number. My professor said so╮(╯▽╰)╭and I think it's reasonable. but I don't understand why ".1" is a number either..........


  • 0
    X

    agree the problem should be more clear.


  • 0
    W
    This post is deleted!

  • 5
    S

    For new commenters: please keep in mind that this post was made over a year ago, when LCOJ didn't yet support customized test cases and print debugging, and only had half the number of questions as it does now. With the new customizable test case functionality, OJ effectively improves the clarity of the problem.


  • 0
    M

    -0 is true ??


  • 0
    A

    Totally agree, I though at the first: where is the clear statement?
    My second thought: Evrika! You could just you try .. catch.

    class Solution {
    public:
    bool isInt(string s) {
        try {
            long long l = stol(s);
        } catch (exception& e) {
            return false;
        }
        return true;
    }
    
    bool isDouble(string s) {
        try {
            double d = stod(s);
        } catch (exception& e) {
            return false;
        }
        return true;
    }
    
    bool isNumber(string s) {
        return isInt(s) || isDouble(s);
    }
    };
    

    but it WA on the test "3e" - which corrent number according to somebody but not a number accourding C++.


  • 0
    J

    Since " 005047e+6" is also true (and "005047e+6 " is true as well), you should have "(\s)*" at the front and back of your regex (damn why I would be so serious to this stupid question :/ )


  • 0

    agree, it cost me a lot of time..


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.