December 21, 2012
Quick Sort

Here is a Quick Sort implementation in Objective C.

-(NSArray*)quickSort:(NSArray*)unsortedArray

{

    NSInteger numberOfElements = [unsortedArray count];

    if(numberOfElements <= 1){

        return unsortedArray;

    }


    NSNumber* pivotValue = [unsortedArray objectAtIndex: numberOfElements/2];
    NSMutableArray* lessArray = [[NSMutableArray alloc] initWithCapacity:numberOfElements];
    NSMutableArray* moreArray = [[NSMutableArray alloc] initWithCapacity:numberOfElements];
   

    for (NSNumber* value in unsortedArray) {
        if([value floatValue] < [pivotValue floatValue]){
            [lessArray addObject:value];
        } else if([value floatValue] > [pivotValue floatValue]){
            [moreArray addObject:value];
        }

    }


    NSMutableArray* sortedArray = [[NSMutableArray alloc] initWithCapacity:numberOfElements];

    [sortedArray addObjectsFromArray:[selfquickSort:lessArray]];

    [sortedArray addObject:pivotValue];

    [sortedArray addObjectsFromArray:[selfquickSort:moreArray]];


    return [sortedArray copy];

}

November 28, 2012

Shut up and take my money!

November 23, 2012

This is pretty neat.

November 15, 2012
FizzBuzz

I started reading this post and realised I’d never solved FizzBuzz.

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Here’s my solution. It’s not perfect, and it’s written in Objective C but uses the console as an output, simply because I happened to have Xcode open at the time I was writing.

for(int i=1; i<=100; i++){

    NSString* output = @”“;

    BOOL isFizz = !(i%3);

    BOOL isBuzz = !(i%5);

    if(isFizz){

        output = [NSString stringWithFormat:@”%@Fizz”,output];

    }

    if(isBuzz){

        output = [NSString stringWithFormat:@”%@Buzz”,output];

    }

    if([output isEqualToString:@”“]){

        NSLog(@”%d”, i);

    } else {

        NSLog(@”%@”, output);

    }

}

October 8, 2012
Optimising SQL databases

I’m currently working on a project which has a fairly large database (~100,000 records) propping it all up. It’s still in development and I wasn’t the original designer of the schema.

Here’s a frightening compharison!

Without index on `id`:

mysql> DELETE FROM `table` WHERE `id`=’xxx’;

Query OK, 3813 rows affected (15 min 58.12 sec)

and:

mysql> INSERT INTO table(…) SELECT … FROM `other_table` WHERE `id` = ‘xxx’;

Query OK, 3813 rows affected (5 min 55.82 sec)

Records: 3813  Duplicates: 0  Warnings: 0

With index on `id`:

mysql> DELETE FROM `table` WHERE `id`=’xxx’;

Query OK, 3813 rows affected (0.14 sec)

and:

mysql> INSERT INTO table (…) SELECT … FROM `other_table` WHERE `id` = ‘xxx’;

Query OK, 3813 rows affected (0.12 sec)

Records: 3813  Duplicates: 0  Warnings: 0

Makes a good case for thinking carefully about your tables!!!

October 8, 2012

Rethink your problem to simplify things for your end users.

September 13, 2012
"When I’m introspective about the last few years I think the biggest mistake that we made, as a company, is betting too much on HTML5 as opposed to native… because it just wasn’t there. And it’s not that HTML5 is bad. I’m actually, on long-term, really excited about it. One of the things that’s interesting is we actually have more people on a daily basis using mobile Web Facebook than we have using our iOS or Android apps combined. So mobile Web is a big thing for us."

— Mark Zuckerberg @ Disrupt SF 2012. (via tobie)

September 7, 2012
How Google makes its maps

Here is a pretty darn good article about how Google uses all the data at hand to generate THE most perfect maps possible. 

http://www.theatlantic.com/technology/archive/2012/09/how-google-builds-its-maps-and-what-it-means-for-the-future-of-everything/261913/

I really like how they are so data driven, extracting as many conclusions and as much inference as possible from everything they can possibly use, and only then do they bring in humans to ‘touch up’ the final product. 

August 20, 2012
Embed Images in Structured Data

I’m currently writing a feature which allows a user to press a button in a mobile app and generate a PDF snapshot of all the data saved in the app, which stores memories. 

I’ve written APIs previously in the ‘proper’ way, crafting a multipart mime form and adding binary files (mainly images) separately to the structured data.

This time round, because there is potential for hundreds of images (bandwidth nightmare but that’s not my concern at the minute) and so matching images up to the section/memory they belong to could be tricky.

With a spark of inspiration I decided to embed the images directly in the structured data, encoded as base64, creating a very atomic interface with the API and eliminating my issues of matching the image the memory. 

I’m sure this isn’t an original idea, but I enjoyed the simplicity of it. Here’s how to create the base64 representation of the image.

There are plenty of implementations of image resizing in Objective C, I’ll not clutter this post with it.

Hopefully you’ll take something from this, even if it’s just ‘thinking outside the box’ to solve a problem in a creative way.

August 6, 2012
The Way a Fast Food Restaurant Should Work

Here is an excerpt from a Wired magazine article about the next generation of fast food restaurant. It gave me goosebumps. 

“Unlike the sit-down bistros where gourmet food is generally prepared and served, Lyfe sees each brussels sprout as merely a cog in a vast clockwork, a system that is set into motion as a customer approaches the counter, gives their name, and places an order. Once that order is sent electronically to the kitchen, a cashier hands the customer a coaster. RFID strips beneath every table pick up the signal from the coaster and send it back to the kitchen. That’s how the runner—someone other than the person who took your order—knows where you are sitting, what you have ordered, and your name.

Now that the order has gone into the kitchen, the software-based cooking system kicks in. It’s smart enough to separate the elements of your order and send each of them to the monitor that hangs above the relevant food-prep station. The flatbread maker sees flatbread orders; the pantry chef, who makes all the salads and desserts, sees the salad order; the rôtisseur at the broiler station—you get the picture. So everything everyone needs to cook shows up in a queue, and the chefs each hit a plastic button beneath the screen to signal that they have begun. When they’re done, they press a button that “bumps” the food order to the “quarterback,” who gathers the finished product and puts it on a plate with all the other stuff you want to eat.

Take that free-range chicken dish. Just as in a high-end kitchen in Chicago or San Francisco, the rôtisseur here cooks the chicken breast at a different spot from the one the legumier uses to sauté the brussels sprouts, squash, and cranberries. Of course, the two elements—the chicken and the vegetables—have different cooking times. Lyfe’s software is ready for such complication, though, and sequences the timing. In the case of salmon and potatoes, say, the fish takes five minutes while the tubers take two. So the rôtisseur receives the salmon order three minutes before the potatoes order appears on the legumier’s screen. Same for the chicken and brussels sprouts. It’s all finished at the same time and plated together by the quarterback. Then the runner picks it up, heads to your table, and says hi, Fred.”

Liked posts on Tumblr: More liked posts »