Extra Cheese

A Blog


String Calculator Kata in Python

Jan 06, 2010

For those who aren't familiar, katas are those impressive sequences of movements that you've surely seen martial arts guys perform. Code katas are the same idea applied to writing code: you solve a problem many times, mastering the movements, and then perform it for others.

My friend Corey Haines has worked with some other people to run a Katacast site dedicated to code katas, posting them roughly once per week. Recently, there's been a string of solutions to the same problem, with my Python version as this week's entry.

Briefly, here's the problem I solve. I have numbers coming in as a string, separated by commas or newlines. My job is to add those numbers and return the sum. There are two complications:

  1. If the first line of the string is of the form "//*", then * is also a possible delimiter. This works for any string, not just *.
  2. Negative numbers must be rejected.

You can see the kata and read my brief commentary on the Katacasts site, or go straight to Vimeo to watch it. It's only 4:32 long, so it's not a big commitment.

String Calculator Kata In Python from Gary Bernhardt on Vimeo.

My screencasts always prompt questions about my Vim configuration, so take a look at my dotfiles repo if you're interested. For this kata, I'm slowing down intentionally – typing slower and inserting small, regular pauses so the viewer has time to look around a bit. I may post a "hard mode" version at full speed if people show interest.

Comments are encouraged, of course – the purpose of a kata is improvement!

If you like this, by the way, you may also enjoy the refactoring screencast I posted recently.



Showing 12 comments

Posted by Nasim at Wed Jan 6 13:38:44 2010

Wow! That is really great. Could you please make a video on how do you configure your vim to do those awesome things.


Posted by Marius Gedminas at Wed Jan 6 14:44:27 2010

I think that was the best screencast of any kind that I've ever seen.  Awesome!


Posted by keanuf at Wed Jan 6 15:12:58 2010

really nice screencast, thank you


Posted by Bernd Schiffer at Wed Jan 6 16:02:33 2010

Nice performance! Congrats!

Two questions after first viewing your kata:

  * On vimeo you mentioned the test runner Mole you used in your video. I saw the test messages in your kata in the red bar, but I haven't seen any kind of stack trace. How do you know which test failed? Maybe this is only due to the kata where you'll almost never will a failing test during a refactoring. In real life you would want to know which and why tests failed, right?

  * Line 10 is a special case of line 17. This is not DRY. I bet you could get rid of line 10.

Keep on the katas! :)
  Bernd


Posted by matt harrison at Wed Jan 6 16:05:44 2010

Very nice.  My only suggestion would be to use something like keycastr, you we can see what you are typing.  I'm sure vimers out there would love it.  Of course, maybe since you are typing 200 wpm, it might just be a blur on the screen.


Posted by matt harrison at Wed Jan 6 16:06:42 2010

hit submit too fast

s/you we/so we/


Posted by Gary Bernhardt at Wed Jan 6 18:18:01 2010

Thank you all for the kind comments and feedback!

Nasim, I'll probably do a vim video at some point since people keep asking for one. :)

Bernd, I actually do have feedback on the failures. If you look closely, you'll see that Vim is jumping me to the line where the failure originates, and printing the failure message at the bottom of the screen. It goes by fast in this video, so it's easy to miss. In other screencasts (like the refactoring one I linked in this post), I mention that explicitly. This one had no talking so I couldn't explain it. :)

Matt, The duplication in the two string.replace lines did cross my mind, but I didn't end up doing it for some reason. That's a very good catch.

I actually did almost every other recorded run with an on-screen keyboard. I would review them on my TV while eating lunch, and I found the keyboard very distracting so I stopped using it. I wish that I could record screencasts with a keyboard that's toggleable by the viewer; that would be ideal.


Posted by Bernd Schiffer at Wed Jan 6 20:16:27 2010

Gary, now I saw your refactoring screencast and I saw again your katacast after that, and now I understand and could see how Mole works :) Thanks!

For the duplicated line: One solution could be to put all the responsibility to _normalize_custom_delimiter as a default parameter like delimiter_spec='\n'. With a little bit rearranging that should do it. One less LoC :)


Posted by Steve Howell at Thu Jan 7 14:01:36 2010

Nice work, Gary!

One kata that I'd really like to see is an example where you start with 100 lines of code already written, and then you have to add features to that.  It would show the different forces at play when you are starting with a slightly larger codebase.

Of course, making such a kata work on the screencast would be challenging, since you can only show one screen at a time.  Solving the problem for the screencast would require a lot of thought, but it would actually mean solving a more general problem that developers have, which is that we only read one screenful of code at a time.  Good failing tests will often help you navigate to the code you need to change, but then there might be other times when you want a more bird's-eye view of the code.


Posted by Ryan at Thu Jan 7 21:32:33 2010

A lot of fun - thanks!


Posted by Dave Foley at Wed Jan 13 17:54:56 2010

I like this a lot.

I did this kata (in javascript) and I never thought to just replace the custom delimiters with commas... that's much simpler than my solution.


Thanks for posting it.


Posted by Nasim at Mon Feb 22 10:50:03 2010

OT:
I am still waiting for a vim cast! Specially the refactoring and auto test runs. Please!


Name:


E-mail:


URL:


Comment: