Update And Problem Solving

First off a quite update. I got two days into writing a new post every night and then had a small family emergency on the third night in so I put a temporary hold on them. I am starting them back up again now.

Now for the fun bit. Problem Solving. This post is not all about the process of problem solving and the processes you can apply, it’s more about just using your brain.

This post came about because I have been reading Write Great Code: Volume I: Understanding the Machine: 1 (it is very good, I would highly recommend it). I was in the mindset of write the most memory effiencent and fastest code and algorithms I can. This lead me down a rabbit hole that took me longer than I care to admit.

I had a simple function to write, take an integer, add a space for every 3 digits starting from the lowest digit in the integer. Basically count backwards from the end 3 steps, add a space, count to 3, etc. Actually very simple(in theory).

Not wanting to use more variables than I needed I was writing code like:

function getNumberWithSpaces(number){
    //Declare the return string
    var returnString  = "";
    //Get a string of the number for easy convertion
    var numberString = number.toString();

    for(var i = numberString.length - 1; i >= 0; --i){
        //If i is divisible by 3 add a space
        if(i % 3 == 0){
            returnString = " " + numberString[i] + returnString;
        }
        else{
            returnString = numberString[i] + returnString;
        }
    }

    return returnString;
}

This obviously didn’t work. I spent a while adding 1 to i and taking 1 away from i without seeing the actual problem. Counting from the highest point, means the i counter does not start at 0. It starts at a number potentiall not divisible by 3. Throwing out the whole return string by the remainder of the original number divided by 3. After mulling this over for a while, I took out my pen and paper and started writing the process out step by step from scratch (something I do to help me better understand the problem). I realised I could have carried on down this route, but counting from 0 upwards but always taking the length - i or somethings letter, I’m not 100% sure how this would work because I chose a much more elegant solution, all for the memory price of 1 integer (could have been 2 bit, or 1 byte but it was in javascript).

The simple solution is to add a counter, if the counter equals 3, reset it to one. Otherwise add one to the counter. Super clean and easy to read, maintain and test.

Here is something like the final product:

function getNumberWithSpaces(number){
    //Declare the return string
    var returnString  = "";
    //Get a string of the number for easy convertion
    var numberString = number.toString();

    var counter = 1;

    for(var i = numberString.length - 1; i >= 0; --i){
        //If i is divisible by 3 add a space
        if(counter == 3){
            returnString = " " + numberString[i] + returnString;
            counter = 1;
        }
        else{
            returnString = numberString[i] + returnString;
            ++counter;
        }
    }

    return returnString;
}

I hope you learned something valuable from this. Everyone messes up on easy problems, pen and paper is still useful and sometimes the simple solution is staring you in the face you just have to bother to look!

Thanks for reading!