[Hacker Rank] Simple Array Sum

From Hacker Rank.

This was a simple challenge but I decided to save what I wrote anyway. I like to take opportunities to practice TDD when I can.

I knew the input would be:

6
1 2 3 4 10 11

The puzzle would be to sum up the values in the list. There’s some stuff with stdin and stdout I don’t get, and they use list comprehension to generate the array, which is still hard for me to follow.

make-the-string-into-an-integer-for-every-string-object-in-the-string-stripped-of-whitespace-but-separated-into-list-elements-by-whitespace

Oh, I get it.

I wrote my test method. I didn’t actually input unittest so this is just my kind of half testing. I had it print out the expected answer (31) and whatever comes out of a method I called find_sum_of_values. Which I hadn’t written yet.

Then I wrote find_sum_of_values and just entered pass.

def find_sum_of_values(arr):
    pass

Then ran it. Obviously, it failed. But that’s what I wanted. You fail first and then make it work in TDD.

After that it was pretty simple as far as Hacker Rank challenges go (I guess that’s why I only got 10 points). I just did a for-loop for every value in the array and added it up in a total. Returning that, and that’s it.

Still, I find that I really enjoy doing it test first.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s