Singletasking

In recent years, I’ve become increasingly concerned about my scatterbrained-ness. I find it hard to focus on a single task for a prolonged period of time: for example, I find it takes me far longer to get through a book, or even read a short passage of text, than it ever used to. I don’t listen to the other person when I’m having a conversation — or rather, I try to listen, but the information somehow always seems to pass through my brain without being stored anywhere. I’m not really present in the moment. My brain gets distracted very easily, and I’ll swear I hadn’t had that conversation with you. My comprehension of texts is perhaps worse than it’s ever been, too. Not all the time, but often, I have to concentrate quite hard in order to actually take in the details of what I’m reading if I want anything more than a cursory understanding. When using the computer at home, I never seem to do anything productive — just follow an endless cycle of checking my feed reader, my e-mail, Twitter, and flicking through my open tabs. Sure, I get things done, but slowly, and piecemeal. My concentration usually feels scattered, my focus divided, my brain like it’s trying to juggle too many balls/clubs/knives/porcupines at once.

Continue reading “Singletasking”

Pea & Mint Soup

image1

Tonight I cooked up a really tasty pea soup. I made it up as I went along – fortunately it turned out really tasty! Here’s my recipe (this made enough for about 2 large bowls):

  • 1 clove garlic, crushed/chopped
  • 1/2 onion, chopped
  • 1 small potato, quite finely chopped
  • 250 – 300g frozen peas (I used 275g)
  • fresh mint, torn; I used one really really good sized handful. Don’t be stingy, it can take a lot!
  • A good squeeze of lemon juice
  • 500ml vegetable stock
  • Some oil for cooking
  1. Heat the oil in a pan at a medium temperature. Add the garlic and onion, and cook until the onion starts to soften. Add the potato and cook a little longer, just to start it off.
  2. Add the stock, cover, and simmer until the potato’s fairly soft (10 minutesish?)
  3. Add the peas and the mint, stir, cover again, and cook until the peas are just soft.
  4. Scoop out a small amount of the peas with a slotted spoon / spatula (you can stick them back in at the end when you serve it).
  5. Blend until smooth. You may need to add a little extra water to get it to a consistency you like.
  6. Season, and add a good squeeze of lemon juice – adjust to taste.
  7. Spoon into bowls, and garnish with the peas you took out earlier (it’s best to lower them in on a spoon, or else they just seem to sink) and a little more chopped mint.

Enjoy!

OS X on a Dell Mini 9

These days, my primary computer is a 15″ MacBook Pro. It’s an amazing machine, but sometimes those 15″ can be a little unwieldy (yes, that’s what she said).

Apple used to make a small, very portable laptop: the 12″ PowerBook, which was unfortunately retired during Apple’s switch to the Intel platform. However, the transition did introduce the potential ability to run Mac OS X natively on non-Apple hardware. Whilst Apple don’t (currently) make their own netbook1, a big community has sprung up around running OS X on non-Apple netbooks. I should note from the outset that installing OS X on non-Apple hardware potentially violates the OS X EULA. If you’re going to do this, in the very least ensure you own a legal copy of Mac OS X Leopard.

Continue reading “OS X on a Dell Mini 9”

Building Ruby 1.9 on Mac OS X 10.5 (Leopard)

I’ve recently been learning Ruby, and I wanted to install Ruby 1.9 on OS X to try out some of the new features (the current ‘stable’ version of Ruby is 1.8.6). Unfortunately, I couldn’t find one cohesive guide to doing this, and instead had to mash together various instructions from across the interwebs. I’ve pieced everything together here in the hope that it’ll help out someone else. 🙂

Continue reading “Building Ruby 1.9 on Mac OS X 10.5 (Leopard)”

Swapping Variables

I thought I’d share a little trick I learnt the other day regarding swapping variables.

A common coding/computer science task is to swap the values of two variables. In some languages, such as Java, one may start by creating a new temporary variable. For example (assuming we already have two ints, x and y):

int temp = x;
x = y;
y = temp;

This creation of a new variable uses more memory, and whilst in most circumstances would be fine, it could become an issue if we’re swapping many times, for example in a sorting algorithm.

Some languages allow us to do a nice swap without extra variables, using multiple assignment and tuple packing and unpacking:

x, y = y, x

This is pretty neat, although is apparently slower than using a temporary variable.

Anyway, yesterday I discovered a third method: XOR swapping. Again, this doesn’t require the use of an extra variable, and I thought it was pretty clever (here ^ represents XOR, as in Python):

x = x ^ y
y = x ^ y
x = x ^ y

Neat, huh? It’s fairly simple to see how it works if you simply walk through the steps. Let’s swap x = 7, y = 9:

x = 0111
y = 1001
x = x ^ y = 1110
y = x ^ y = 0111
x = x ^ y = 1001

Apparently this method is quite commonly used in embedded assembly code, however you won’t find it appearing much in code on a desktop computer. It usually is slower to compute than using temporary variables, as most processors attempt to execute commands in parallel – and of course, each of these instructions depends upon the previous result, so they must be executed sequentially. Still, it’s a nice thing to know.

The short version: it seems that if you’re trying to save memory then XOR swap or use multiple assignment, otherwise if efficiency is an issue, use a temporary variable.

FizzBuzz Follow-up

I thought I’d post a follow-up to yesterday’s FizzBuzz post with some sample solutions, as a bunch of you seemed to find it fun to solve. Feel free to add your own solutions in the comments of this post!

My own first solution was Python-based, and a fairly simple one:

for i in range(1,101):
    if i % 3 == 0 and i % 5 == 0:
        print "FizzBuzz"
    elif i % 3 == 0:
        print "Fizz"
    elif i % 5 == 0:
        print "Buzz"
    else:
        print i

Obviously it’s not in a function or anything, but I just wanted to do it as fast as possible. Shortly afterwards, I realised that line 2 could be simplified by changing it to:

if i % 15 == 0:

Because of course, if a number is a multiple of 3 and 5, then it must be a multiple of 15.

Here are a couple of nice alternatives (ideas courtesy of Dave, although re-written by me because we didn’t save them). First, writing FizzBuzz as a iterator:

def fizzbuzz_generator(n):
    for i in range(1, n+1):
        if i % 15 == 0:
            yield "FizzBuzz"
        elif i % 3 == 0:
            yield "Fizz"
        elif i % 5 == 0:
            yield "Buzz"
        else:
            yield str(i)

for result in fizzbuzz_generator(100): print result

Then, a simple function to calculate the correct fizz/buzz, combined with a list comprehension to create the result (man, I love list comprehensions):

def fizzbuzz(i):
    if i % 15 == 0:
        return "FizzBuzz"
    elif i % 3 == 0:
        return "Fizz"
    elif i % 5 == 0:
        return "Buzz"
    else:
        return str(i)

print "\n".join([fizzbuzz(i) for i in range(1,101)])

Finally, here are two neat little Ruby one-liners, taken from the comments of the original FizzBuzz article. I hope the authors (Brendan, and Brian respectively) don’t mind me reproducing them here:

puts (1..100).map{|i|(s=(i%3==0?'Fizz':'')+(i%5==0?'Buzz':''))==''?i:s}
puts (1..100).map{|i|i%15==0?'FizzBuzz':i%5==0?'Buzz':i%3==0?'Fizz':i}

How neat is that? Anyway, as I said, feel free to leave your own solutions below.

FizzBuzz and Other Problems

Update: Check out the follow-up post for some sample solutions!

I love to code. Since a young age (I think I was about 7 when we first got our BBC B, and I would patiently tap BASIC code into the blinking prompt from childrens’ books about programming), I’ve loved to write programs. I read somewhere recently (unfortunately, I forget where) that it’s not the act of coding itself we love, but rather it’s solving problems that we love. I think that’s one reason I like learning new programming languages, as it allows me to solve problems in different ways.

A while back, I came upon the FizzBuzz idea. In short, it’s a simple exercise for employers to give to potential employees at interviews; a very simple exercise, yet one that someone will only be able to answer quickly if they ‘get’ programming. A suggested FizzBuzz problem is the following:

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’.

I invite everyone reading this to have a go now – time yourself, and see how quickly you can do it. Unfortunately, I only timed myself by looking at the clock, but I was glad to have completed it in under a minute. Apparently this shows I can code:

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.

Want to know something scary? – the majority of comp-sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

That is a scary thought, but I expect it’s almost certainly true.

So, this sort of simple problem excites me – I can work out a solution, and then try and improve that, perhaps. Myself and Dave spent a fair while earlier today trying to come up with more and more sophisticated solutions. And y’know what? It was fun!

On a similar note, I thought I’d mention a website that I found a while back (thanks to Matt Gwynne) that might be of interest to anybody else who enjoys these kind of problems. Project Euler is “a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve”. It contains a list of (at the time of writing) 195 mathematical problems, and the aim is to write code to solve them. You can then put in your answers, the website will tell you if you’re correct, and check them off the list. Great fun, and it’s neat to see some of the interesting solutions that people come up with (and the range of languages they use!). Hooray for being a geek! \o/