# Day 88 - Diversion: Cheating at Katas In which we potentially miss the point of the exercise.

Putting the beholders to the side for a bit, let’s talk about practice problems.

## Switches

Many years ago, I used to do a lot of programming practice problems (what one coworker calls “katas”, and another calls “interview questions”). I even got into Project Euler for a bit while I was learning Ruby; I worked on each question until I could craft a solution that took less than a second to run, until I started hitting questions where that was actually impossible.

So the other day, I ran into a fairly standard one while brushing up on my Python. You have set of n switches, and some defined sequence of toggling them, and at the end you need to describe the final state of the switches. In this case it was described as follows:

“There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.”

I know I was supposed to sit down and puzzle out the mathematical sequence (the first bulb is toggled only once, the second twice, the third twice, the fourth three times, hmmm), but it was the end of a long day at the day job and it just wasn’t coming to me.

## Brute Force

The brute force solution in this case is to fully simulate the process. Efficiency be darned, let’s just do the thing.

Ok, so that works.

## Spot the Pattern

Humans are fairly good as spotting patterns, so let’s do a bunch of runs and see what emerges:

With m of 11, we see:

…and I think I see the pattern. Is it that simple? Let’s tweak the code to show us only when the result changes:

And with an m of 101, we now see:

…and now it’s super-clear.

## Simplicity

The efficient solution for this version of the switch problem is:

Is it valid to use the brute-force simulation to show the way to the elegant math solution? It certainly had me writing more code than if I had just puzzled out the equation. And maybe that’s what I wanted to do anyway.

## Useful Stuff 