Disclaimer: I was recently asked this in an interview and quickly found myself dealing with some very messy indexing syntax using arrays. I decided to revisit the problem later to see if my initial hunch of treating the uint input as a string would tend toward a cleaner solution.

/// <summary>
/// Input treated as a string for simplification 
/// </summary>
/// <param name="x"></param>
/// <returns>bool</returns>
protected bool IsPalindrome(uint x)
    string givenNum = Convert.ToString(x);
    char[] input = givenNum.ToCharArray();    

    string testString = String.Empty;
    foreach (char a in input)
        testString += a;

    if (givenNum == testString)
        return true;
        return false;

It works fine but I still thought it was a little more verbose than it could be so I posted this question on Stack Overflow and got some very interesting responses. I eventually went with the following solution; since a uint was specifically specified as a parameter, it would seem that an interviewer would be looking for a numerical, non-string answer.

protected bool IsPalindrome(uint x)
    uint original = x;
    uint reverse = 0;

    while (x > 0)
        reverse *= 10;
        reverse += x % 10;
        x /= 10;
    return original == reverse;

Tags: ,


ASP.NET Programming Tips - Tip #2

by agrace 30. September 2010 22:39

Double your BlogEngine Traffic in 60 Seconds

BlogEngine SEOSome time ago, I blogged about the increasing importance of performance, read page load speed, in the determination of "rank" by Google.

Then I noticed that most of my blog posts included an image and that there was a considerable lag, so I decided the quickest method of fixing this was to simply go into my settings and decrease the number of posts per page from five to three.

In just five months, the number of hits per months has gone from 9,977 for May to 13,542 for September, a 35% increase. In that time period, there were only three blog posts put out. Draw your own conclusions!

Tags: ,


ASP.NET Programming Tips - Tip #1

by agrace 26. September 2010 09:20

Google Cannot Solve your Coding Problems

Using GoogleStop and think before you do this. Googling for code samples is usually the final step when designing solutions. Start by defining the problem; if you can't define the problem, how can you solve it? Then, and only then, start working on a black-box solution of your own; identify the basic inputs, general logic (phrase it in a single sentence - this is the black box), and outputs of this black box. Even if you're not artistically inclined, always opt to take pencil to paper - it will help.

Finally, with a clearer perception of what it is you need to do, use Google to focus in on relevant code samples. Doing this in reverse order leads us to try and re-imagine the problem to fit somebody else's solution. I imagine 80/90% of programmers, myself included, are guilty of this everyday form of insanity.

kick it on DotNetKicks.com