String Calculator

This is an example how interactive process in TDD can help you think and expand on requirements.

Here are the concrete requirements:


Before you start:

Try not to read ahead and do one task at a time.

Create a simple String calculator with a method int Add(string numbers).

The method can take a string representing numbers separated by commas, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”.

1. Start with the simplest case of an empty string and move to 1 and 2 numbers.

2. Allow the Add method to handle an unknown amount of numbers.

3. Allow the Add method to handle new lines between numbers (as well as commas).

a. The following input is ok: “1\n2,3” (will equal 6).

b. The following input is NOT ok: “1,\n”.

4. Support different single character delimiters (case-insensitive).

a. To change a delimiter, the beginning of the string will contain a separate line that looks like this: “//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where the delimiter is ‘;’.

5. Calling Add with a negative number will throw an exception “Negatives not allowed” - and the negative that was passed in. If there are multiple negatives, show all of them in the exception message.

6. Numbers greater than 1000 should be ignored, so adding 2 + 1001 + 13 = 15.

7. Allow multiple delimiters like this: “//[delim1][delim2]\n” for example “//*%\n1*2%3” should return 6.

Doing one task at a time suggests approaching the problem interactively - one task at a time - one test at a time and then expand on the tests.

Starting with the simplest case - returning 0 if empty string.

Writing this first test, the visual studio will suggest you don't have StringCalculator class created - the SUT- system under test. So that's what we need to create first.

This is enought code so the two projects - the test project and the class library project containing the SUT can be copiled. Well they are now, but the test fails. Lets write the code that fixes the test.

This is quite simply returning 0 if the string is empty, which makes our first test pass.

You can have a look at https://github.com/velchev/CTCalculator where I have added commits on every step of the iterative process, just so you can see the way of thinking about the problem.

The next requirement is when we have a single number in the string to return the same number as a result

Here I used Autofixture for xUnit. This is just to simplify the Arrange phase of the tests and to focus more on what the test is. It supplies SUT and random positive number. I am preparing the input and asserting on the result,  very clear intent.

Again this will fail as we haven't implemented this logic yet.

The way you can think here is if is not in the test we are not writing any extra code. This basically drives the TDD - tests needs to drive the implementation and not anything else. In this way no logical path is hidden, no surprises in the way the system works and clear documentation of what SUT does.

You can go through the commits of the repository for all the further steps. It was nice to implement this pure example of TDD. Some may argue that you can even think about requirements as user requirements and demonstrate BDD here, but they are so technical, there is no user to be part of the story which translates to meaningful text.

The next time you are given story write BDD style requirement and then on the implementation of specific method write some TDD style code and you will see it allows your thinking to focus on what you need to implement and not how till you need to implement, but by the time you need to implement the failed test you will know exactly what you need to write to pass it.

I hope you enjoyed this small post. Noting unique, but is my own interpretation of an Interview Task which I did recently.



Good Web Sites to train your algorithmic skills:

Site for collaborative coding: http://collabedit.com
Source Code of the most popular data structures and algorithms in JAVA http://code.google.com/p/java-algorithms-implementation/

All the algorithms are implementations of algorithms described in Introduction to Algorithms book A web page useful for online interviews: http://collabedit.com It allows two or more people to work on one code together, by just knowing the exact address.


How to change the SSD of Mac Book Pro Late 2012

I bought my Mac Book Pro with the best parameters at its time - CPU, GPU - Video Card, Maximum allowed RAM Memory 16GB, but the only thing I made a compromise was the SSD. At that time to buy 512 GB instead of mine 256GB would cost me additional £600. I remember the overall price of the laptop was £1700 with student discount (I was a student in the University of Bristol at that time), otherwise would be more than £2000 (can't remember how much exactly). Anyway the compromise was made and after just a couple of months it proofed itself to be very a bad one - too little space - this means nothing except programs and VMWare - Windows 7 (with Microsoft Development Package Visual Studio, SQL Server and so on) and X-Code. It was a struggle. I am a Software Engineer and usually use my laptop only for work but still I wanted to listen to music, edit home videos and other staff.

In 2014 I bought this storage expansion card - SDHC:

from Amazon. That was quick and cheap solution 128GB for 80 USD.
So now I could actually store images, videos and music. But it was slow and not as fast as SSD and for sure not useful for running applications directly from there.

The last option for me was fixing the actual problem - change of SSD. That was difficult choice as it was not officially done by apple.

After a lot of googling around I decided not to do it. This year - 2016 after I sow the new MacBook pro's - the fact that they still have 16 GB RAM, not so much better processors and most importantly the price was close to 3000£ if I wanted better components, I decided I will try to postponethat buy for some years.

So my next step was - go to amazon buy SSD and change it yourself.

It was £450

Then it turns out that is quite an easy process - everything is written as part of the instructions that are coming with the memory. First from system recovery mode copy the OS on the new SSD. Open, Replace and you are ready. It turns out that is exactly this. Do not be afraid!

Some pictures of my mac during the process:

Step1: place it and prepare for change so that each screw can be placed next to it's positions. The screws are 3 types (I am not sure), so not to care place them next to the whole where they have to go to.

Step2: Remove the screws and clean the dust with cotton buds. If you have never open your mac you will find a lot of staff in there.

That was it. After I replaced the SSD I careful put back the screws in their exact positions and all done. It was working fine, although it took some time for the spotlight search to reindex the files - couple of hours. Since then it was running absolutely fine. It is worth testing with Disk Speed Test - software for mac before you change your SSD and after - to compare if the new one is slower or faster. For me (Mac Late 2012) it turns out that there is no difference. Just when you test make sure you have not much running apps - so that the whole RAM can be used to test the SSD.

I know that 2016 Mac's have some unbelievable Read and Write Speed of their SSDs, but till the price is that hight I will be waiting for some years again before making change.

That's my Mac Now - from 256 GB to 1 TB:

Hope you get just a little inspired and go and change the SSD instead of selling what you have and buying a new model for more than £3000.