Ad
  • Custom User Avatar

    A reasonable optimizing compiler will produce the same machine code regardless of whether the variable is defined before the if-check or after it. Initializing local variables has no effects visible to callers so the compiler may freely reorder the code as it sees fit.

    The reason is just style and clarity. First of all, it's common to add preconditions to the start of a function. If there are no items, we already know the answer and we can return right away. Second, it's generally a good idea to keep the scope of variables as small as possible.

    Of course, in such a short function it doesn't really matter, but if a long function defines variable in the very beginning and uses it a lot later, can you be sure that there are no uses between? You have to carefully read through the whole function to check if there are other references.

  • Custom User Avatar

    Yes, that's correct and certainly a bug. In extreme cases it might even turn a terminating computation into non-terminating. Annoying n-plus-a-half loop strikes again. Easy to fix, but turns the code into a mess anyway. :)

  • Custom User Avatar

    Yeah, using StringBuffer is the most important thing, since it completely changes the performance charasteristics when there's a lot of data. (As an aside, I wondered for a while why I used StringBuffer instead of StringBuilder, but then I realized that appendReplacement requires StringBuffer.)

    Otherwise it's really just details, but a few things

    First of all, you use the pattern [a-zA-Z] whereas I use \w for the same purpose. You could replace all occurrences with \w. As an added benefit this would then work with non-ASCII characters if you pass UNICODE_CHARACTER_CLASS to Pattern.compile. Not terribly relevant here, but it's a good practice in general not to assume that letter are just a-z. (Personally I'm Finnish, so I use 'å', 'ä' and 'ö' as well.)

    Now this is where it gets even more into the fuzzy territory of personal style: I like to separate the two concerns of splitting words and abbreviating them into different parts that operate with minimal shared assumptions.

    What's the minimal interface for abbreviating words? Of course a function that takes a word and return an abbreviation. That function would be easy to test separately, use somewhere else and in general I think it makes the code more readable by pulling the abbeviation logic out of the processing.

    Of course the code here is so simple that it would be understandable without extra function, but I prefer to err on the side of too clarity in these things.

    So that's generally how I approach these things. Then sometimes I need to make concessions for performance or things like that and I need to make the interfaces less clear. In this case, if performance was a real concern, I'd skip the substrings and regular expressions alltogether and simply write a state machine that loops over each character, detects word boundaries and keeps integer count of letters seen between start and end.

    Anyway, nothing wrong with your approach either, it's just not my style. :)

    Hope these comments helped!

  • Custom User Avatar

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

  • Custom User Avatar

    The message doesn't mean that the way you produce your commands is not optimal, but rather that your command list itself is not optimal. That is, there is a shorter list of commands to the destination.

    The problem description of the problem states that you need to return the shortest path:

    Your task is to write a function, which returns the list of commands, Robby has to do in order to reach the target cell and save as much power as possible.