Scraping a Pass

I received the results from one of my modules from last semester. I’ve already had my results from two other modules and I was looking at a possible first if everything else went well, realistically though I was probably looking to get a solid 2:1.

Then I got this grade back. Wow, it really ruined my day, I did a little better than expected on the assignment but bombed the test. Thankfully my assignment marks put me over the pass mark but just barely. I’m not happy at all with this grade.

Part of me wants to rage at the system. This was a level 6 module and a hard one at that. I’m currently taking the level 5 module that’s supposed to build up to this one. This is the result of years of scheduling and re-scheduling the modules until this was the only fit, it’s a major fault of the school, and one that they’re rectifying next year, but I can’t really blame the school. These things happen with teachers and awards coming and going over the years. However having taken these modules out of order I feel has undermined me, and everyone else on the award. I spent a lot of time trying to figure out the basics of the subject while being taught about the much, much more advanced applications and techniques. It was very frustrating.

All in all though I had plenty of help. I plagued my tutors during this module and I was just happy to have a working application to submit for my assignment in the end.

My study technique failed me tremendously for the exam. The module covered a lot of very complicated subjects over a a fairly short period of time. I didn’t understand a lot of the basic stuff so I focused on those for my general study and then looked to the past exam papers for my more focused revision. I revised for two days solid and was fairly confident when I sat down to take the exam. However, the tutors went for a complete re-write on the exam this year and virtually nothing that had been on any of the past papers appeared on this exam. So I basically sat the exam almost completely unprepared.

Worst case scenario this bumps my degree down to a third class, which is horrifying. If I get solid firsts this semester I can maybe scrape a 2:1 which would be fine. So far I’m doing extremely well in two modules and not as well in the other two so I’ll just have to wait and see how it goes but I’m optimistic but this is going to be hanging over my head for for the next two months, especially come exam time.

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];
}