Ad
  • Custom User Avatar

    You should test that the function returns true when all needed characters are present (i.e. it recognizes all possible uppercase letters - 26 tests, recognizes all possible special characters - another 20 tests), not only that it returns falls when something is missing.

  • Custom User Avatar

    Having programmed with .NET 2.0 most of the time, I never much got into contact with Linq until now - this solution just was the first that came to mind.
    Looking at other solutions on this site I have learned so much more about Linq this week than I would ever have imagined before.

  • Default User Avatar

    Hey-ey!
    Thanks for the analysis. After viewing other solutions I was curious about the relative speed of this simple line.
    10k runs is great but should your test randomly generate sequences of numbers?

  • Custom User Avatar

    This is a bit of a dirty trick on my part. X-P

    There's actually no such thing as a "down-to" operator, it's just creative spacing. What this really is is a post-decrement operator (--) and a greater-than comparison (>) squished together in a while() loop.

    So, it really should be written as:

    while(n-- > 0) {
    

    Because it's a post-decrement, it translates to something like this:

    
    var nIsGreaterThanZero = n > 0;
    n = n - 1;
    while(nIsGreaterThanZero) {
      h += 'hue';
      nIsGreaterThanZero = n > 0;
      n = n - 1;
    }
    

    You should never use this in production, it's more for a laugh. It relies too much on magic, and has an unexpected side-effect of n being one less than you would expect inside the loop. In fact, n will exit the loop being -1, even though the loop was testing against 0.

    I could only get away with it here because the value of n was not used inside the loop.