Ad
  • Default User Avatar

    Maybe I am a bozo but no need to be rude for posting an issue; instead you should give the input and your output.

  • Custom User Avatar

    I don't understand why try/except would be faster. Can you (or more likely, someone else) explain?

  • Custom User Avatar

    To those defending it based on readability, using += on a String in a loop is both a performance anti-pattern and not more readable than using a StringBuilder with StringBuilder.append in place of += on a String.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    The definition of modulo is very ambiguous simply because there are several different accepted definitions. Many call javascript's % a modulus operation. Additionally, I wouldn't consider Javascript modulus a true remainder operator, because it takes the sign after division. Historically, remainders have been annotated as non-negative integers. That's why I provided a definition in the description.

  • Custom User Avatar

    Yeah, I thought the same thing after I saw this (the c/java part)

  • Default User Avatar

    thnx m8 =)

  • Default User Avatar

    I have to agree with SithFire here. It specifically says when you hover over the 'Best Practices' button: "Best Practice solutions tend to be a good balance of performance, readability and maintainability."

    It does sounds like you are only focusing on performance in your comment.

  • Custom User Avatar

    Multiplication is still O(n**2), so...

  • Default User Avatar

    Best practise doesn't mean you have the fastest solution. It's a trade-off among different characteristics of software quality, e.g. time behaviour, changeability, code readability, testability and so on (cf. ISO/IEC 9126). It depends on individual cases. For instance, if this method is called once, this might be the best solution, O(n^2) wouldn't hurt anybody. If this method is called million times, this solution could be a big pain.

    I agree there is a difference between short code and quality code. But there is also a difference between performance and quality code. Since you only talked about time behaviour, I think you mistake performance/time behaviour for quality code (= best practise).

  • Default User Avatar

    Agreeing. Both issues take O(n^2) time, and both operations (checking for multiples vs. single occurrence and appending a result string) are doable in O(n) time.

    If indexOf(x) and lastIndexOf(x) cached their values (since String is immutable), the result would be O(n), but that's not how String is coded as of Java 8.

    The Java compiler optimizes String x += y to the bytecode equivalent of

    x = new StringBuffer(x).append(y).toString();
    

    However, it does not take the optimization a step further to automatically create a single StringBuffer in a loop. That is, it does NOT perform the optimization

    String result = "";
    for (char c : charCollection) { result += c; }
    

    ...to...

    // This optimization DOES NOT HAPPEN
    StringBuffer internalStringBufferForResult = new StringBuffer("");
    for (char c : charCollection) { internalStringBufferForResult.append(c); }
    String result = internalStringBufferForResult.toString();
    

    Prior to doing some research, I thought that's what happened under the covers -- but it's clear that I was wrong.

  • Custom User Avatar

    You can also solve it by initialising it with a seed known to you, saving a random number in guess and then resetting to the seed you chose, at least that seems to be the intention of this Kata.

  • Default User Avatar

    To elaborate: In Java, I get "Method PeopleThatPlayBanjo() should not be static". Removing the Static keyword from the test case makes it work.

  • Custom User Avatar

    Well, if you think your solution is really good enough, you may employ that "cheating" to pass the last test case, and then compare your solution to the original one to find out what's the difference. Noone would blame you. :)

  • Custom User Avatar

    Definitely yes. :)

    The last test is the most complex and the most "real-life" of all.

    In fact, if you pass all the tests except the last one, THAT means you employ some kind of cheating :) by tweaking your solution to work with particular tests, instead of making a universal algorithm. :)

    The simplified K-means clustering algorithm usable for this kata is just about 40 lines of code, not so much.

  • Loading more items...