Kata 3 – Magic Numbers

This kata comes from the website of one of my old lecturers. It was an interesting problems to tackle and there are a lot of different ways to approach it.

159 * 48 = 7632 contains each of the numbers 1-9. The program finds and displays all the other simple multiplications that also contain each of the numbers 1-9.

I started by creating an algorithm that generated a string containing all the numbers I wanted. This was exceptionally tricky to do. So my algorithm just starts counting from 123456789. Each number is checked that it contains only one of each number. Because I’m using only 9 digit numbers there isn’t any need to validate any further than that. If the number is valid it’s passed through to another checker that systematically changes the number into a simple multiplication equation. If the equation is valid I win.

The application takes a ridiculously long time to run. After completion I thought of a way to potentially half the run time but I didn’t implement it because I’ve been obsessing about this for far too long.

This was done largely using TDD but I actually wrote the test in Obj C and then transferred the program to C# because of a memory problem I was having in XCode.

 static void Main(string[] args)  
{
string equationString;
List createdStrings = new List();
//create an array of strings ///159 * 48 = 7632
for (int i = 123456789; i <= 987654321; i++){
equationString = i.ToString();
//validate strings with the checker
if (equationCharacterChecker(equationString)){
if (equationChecker(equationString)){
createdStrings.Add(equationString);
}
}
}
}
public static bool equationCharacterChecker(string equation) {
SortedSet setOfEquationCharacters = new SortedSet();
setOfEquationCharacters.Add("1"); setOfEquationCharacters.Add("2"); setOfEquationCharacters.Add("3");
setOfEquationCharacters.Add("4"); setOfEquationCharacters.Add("5"); setOfEquationCharacters.Add("6");
setOfEquationCharacters.Add("7"); setOfEquationCharacters.Add("8"); setOfEquationCharacters.Add("9");
string character;
for (int i = 0; i < equation.Count(); i++)
{
character = equation[i].ToString();
if (setOfEquationCharacters.Contains(character)){
setOfEquationCharacters.Remove(character);
} else {
return false;
}
}
return true;
}
//159 * 48 = 7632
public static bool equationChecker(string equation) {
int length = equation.Length;
for (int m = 1; m < equation.Length-2; m++){
for (int e = m+1; e < equation.Length-1; e++){
int multiplicand = Convert.ToInt32(equation.Substring(0, m));
int multiplier = Convert.ToInt32(equation.Substring(m, e-m));
int product = Convert.ToInt32(equation.Substring(e));
if (multiplicand * multiplier == product) {
Console.Write(multiplicand + " * " + multiplier + " = " + product + "\n");
return true;
}
}
}
return false;
}

Kata2 Binary Search

I did my second Kata this morning. I thought I’d tackle the binary search problem in following with http://codekata.pragprog.com
The idea here is that you have some huge, but sorted, list and you need to find a single element within that list as efficiently as possible. As lists grow in size just traversing through the list element by element can take a really long time for higher numbers.

The binary search solution basically means you start in the middle continually divide the list in half until you find your element. For huge lists it’s excellent because it doesn’t matter where the desired element is in the list, it will always find it quickly, one drawback though it will more often than not, take several searches to locate the element. So you need to weigh the efficiency lost against the efficiency gained. 

I’m quite proud of my attempt. I’ve tested it with a list of 50,000,000 integers and it never takes more than 26 searches to find any element. It’ll support an array of any size and it should support any data type, but I’ll need to test that at a later time. This is a recursive approach as it was the approach that comes most naturally to me. I’m going to have a look at an iterative at a later time.

This was developed using TDD, kind of. I had to test this from a few different directions at once, namely efficiency and accuracy. I couldn’t work out a good way of testing for them both at the same time so I’m leaving what test code I did end up with out of this post.

 -(NSNumber*)binarySearch:(NSArray*)array forInt:(NSNumber*)anInt {  
int numberOfSearches = 1;
int min = 0;
int max = array.count;
int searchingFor = [anInt integerValue];
int indexOfGuess = [array indexOfObject:[array objectAtIndex:(max + min) / 2]];
int guess = [[array objectAtIndex:indexOfGuess] integerValue];
NSLog(@"%d searches", numberOfSearches);
while (searchingFor != guess){
if (max - min <= 2){
return [NSNumber numberWithInt:-1];
}
if (searchingFor < guess){
max = indexOfGuess;
}
if (searchingFor > guess){
min = indexOfGuess;
}
guess = [[array objectAtIndex:(max + min) / 2] integerValue];
indexOfGuess = [array indexOfObject:[array objectAtIndex:(max + min) / 2]];
numberOfSearches++;
NSLog(@"%d searches", numberOfSearches);
}
return [NSNumber numberWithInt:indexOfGuess];
}

Bruising the Ego

I’ve learned quite about managing your ego when it comes to development. Every programmer has an ego, don’t listen to what they might tell you otherwise. We all consider ourselves the poets of the computing world, thinking up beautifully elegant solutions to impossible problems.

I have a huge ego, I freely admit this. I don’t handle criticism well and I’m a sore loser. I constantly aim to blow peoples minds with my genius and when that doesn’t happen I fall into pits of self loathing and depression. It’s a constant struggle for me.

Two of my apps have received bad reviews recently. I’ve got another app that I’d written for a client that seems to have given up on me all together, bought my app and immediately reporting a bug and requesting all sorts of changes. I fixed the bug right away and then spent a month re-writing the app. Now they won’t respond to my emails, nor have they updated the app. My natural pessimism tells me that they’ve written me off all together, but I just don’t know.

Being that two people have chosen to be vocal about their dislike of my apps, demographically I know that there are a percentage of others out there that feel the same way and have just chosen not to speak up. I spent a lot of time and energy on those apps and I’m very proud of them and their success so far. These were my ideas, my creations, my products and I want everyone to love them.

Now in my defence neither of these two users appear to have read the description of the apps before purchasing, which is their mistake. And the things they’re complaining about are actually restrictions put in place by Apple, nothing to do with me. But these are still my app reviews. There is also information within the apps and on the app store that would allow a displeased customer to get in touch with me of there’s a problem. But they haven’t tried to reach me. I take some solace in knowing, from experience, that there are customers out there that just want to complain about anything that they can. You can’t please everyone right, so why don’t all those that like my apps post good reviews and make me feel better about it. I’m sat here looking at dissatisfied customers and poor reviews and I’m powerless to do anything about it.

I suppose that I could look at it as there’s being an opposing percentage of those that are very happy with my app. There is one good review as well, so that’s something.