SHARE:
Uncategorized

All the thoughts that went through my head…

Flatiron School / 3 July 2013

The following is a guest post by Max Jacobson and originally appeared on his blog. Max is currently a student at The Flatiron School. You can follow him on Twitter here.

all the thoughts that went through my head while trying to pass the first test in the test suite of a warm up exercise this morning before coffee and then again this evening after painkillers

Recently at school we’ve been doing code exercises to warm up in the morning. We’re given contained problems to solve with code. Problems that would be possible to solve without code but tedious or prohibitively time-consuming. Sometimes we move on to lecture before I can solve one and it haunts me all day.

Here’s this morning’s:

Palindrome Product

A palindromic number reads the same both ways.

The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.

Write a program that can detect palindrome products in a given range.

The specifics of what this means were ostensibly made more clear by a provided test-suite.

I’ve done a problem like this before at a coding meetup and I remembered how I’d wrestled with it for minutes and got it working and then seen someone else’s impossibly elegant solution and felt inspired.

Mine was probably something like this:

And hers was something like this:

And I was just like oh.

So this morning I had a moment of thinking I already knew the hard part of this problem, and then I looked at the tests. Here’s the first one:

Oh there’s a lot more going on there.

But that’s fine, I think, I’ll make a Palindromes class, give it a few methods, that’s fine, let’s do it. So I started with something like this:

At this point something about the test bubbled to the surface of my mind.

Wait, what the what is the largest method returning that it has a value method?

Damnit Jeff do we have to create another class?

OK fine I guess palindrome factors are a sufficiently interesting thing that they should be their own class. I kind of reached a point in the generate method where I had some data I didn’t know what to do with, and that makes some sense as a place to put it, so I started rewriting… and ran out of time.

I’m going to finish it now, I have to. It’s very late. I used to stay up this late. Jeez.

Up until this point, running the tests hasn’t felt worth doing, because I didn’t even have the methods it’s trying to call. But now I do. And maybe it’ll even pass.

It doesn’t make them pass.

Before I knew about proper test-driven development, I did a sort of shake-and-bake version where I manually tested things, and my favorite tool for that is CodeRunner which lets me just run little bits of code and see what happens. Kind of like irb/pry but with a GUI. So I copy my code into there without the tests and add this:

And this is what it printed:

I’m always surprised by the dumbest things, but that seems really long to me. That’s not even every permutation of 1..9, it’s just the ones whose product is a palindrome.

Even though it surprised me, I still don’t really see why it’s wrong. So I take a look at the first assertion and try to test it

And it printed: 9. Hey! That probably passed! Shouldn’t it turn like half green or something?

OK fun’s over let’s look at the next test.

And it printed: [9, 1].

For a moment my nose flares because I think this is a bad test and it’s not my fault. Of course it’s not my fault my largestmethod returns the two digits in one order and the test is expecting them in the other order! The way multiplication works, the order doesn’t matter! It should just include [9,1]in that list so my answer will be right! I want to just edit my answer into to the list of acceptable answers.

I want to be clear that at the time of the writing of this sentence I still don’t really know what’s wrong with it but I kind of have a hunch. I’m noticing there are actually way more brackets in that array than I was mentally doing anything with. It’s not an array of arrays, it’s an array of arrays of arrays! But why? I’m going to just look at it for a moment here:

So maybe the order of nine and one matters later, but it’s notwhy I failed this test.

The test asserts that this array includes the largest palindrome’s factors. The largest value is nine — I got that part! — but there are two in-range pairs of numbers that multiply to form nine, and I’m not currently doing anything to group those together. The test is accounting for those two sets of pairs to be grouped in either order, but it wants both. Okay. Now I know.

I made some changes that felt right. I didn’t really think a lot so much as I just vibed out to the new A Great Big Pile Of Leaves album (which has played through maybe three times in full as I write this post) and typed stuff.

Allow me to be absolutely clear: this still does not pass the tests but I’m starting to feel a momentum and a full glass of milk happiness in my head because mysteries are unraveling before me.

When I go back to CodeRunner, where I used to see merely [9, 1] I now see: [[1, 9], [3, 3], [9, 1]] and I know what to do.

I change lines 10-13 of the previous example to:

and run the tests and it passes and I should be weeping happily right now.

After passing the first test it only takes about 30 seconds to get the rest to pass. Here’s my final solution on gist. It’s out of the scope of this post.


Later in the day, Carlos remarked that he sometimes leaves his body while doing yoga. I asked if the same is true for programming but I think I already knew how I felt about that.

I find problems like this very satisfying in that same disassociative, mind-clearing way. When I first start, my mind is moving too fast and I make too many assumptions and just start doing things and that’s probably fine because that’s how I find out what shape it is. I’m reminded of this Steven King quote on writing from On Writing (via):

Stories are found things, like fossils in the ground… . Stories aren’t souvenir tee-shirts or GameBoys. Stories are relics, part of an undiscovered pre-existing world. The writer’s job is to use the tools in his or her toolbox to get as much of each one out of the ground intact as possible. Sometimes the fossil you uncover is small; a seashell. Sometimes it’s enormous, a Tyrannosaurus Rex with all those gigantic ribs and grinning teeth. Either way, short story or thousand-page whopper of a novel, the techniques of excavation remain basically the same.

No matter how good you are, no matter how much experience you have, it’s probably impossible to get the entire fossil out of the ground without a few breaks and losses. To get even most of it, the shovel must give way to more delicate tools: airhose, palm-pick, perhaps even a toothbrush.

Right?? What a beautiful, humble reframing of what makes great creative work: no, you’re not some genius coming up with profound, original ideas; you’re just digging, and gently, and hoping that if you just keep trying stuff, you’ll find something.

Build a Blog-Based Site with RefineryCMS Previous Post Building a Mobile App Landing Page Next Post