Archive

You are currently browsing the Log et al – Peter Curd blog archives for July, 2010.

Jul

25

Twitter Weekly Updates for 2010-07-25

By pcurd

Powered by Twitter Tools

Jul

18

Twitter Weekly Updates for 2010-07-18

By pcurd

Powered by Twitter Tools

Jul

11

Twitter Weekly Updates for 2010-07-11

By pcurd

Powered by Twitter Tools

Jul

4

Twitter Weekly Updates for 2010-07-04

By pcurd

  • Watching the #worldcup so I'll have something to talk about tomorrow… Come on #eng – show the foreigners why we invented the game. #
  • You're not supposed to take the bloody corner flags out! #eng #worldcup #
  • Noooooooo! #eng #worldcup #
  • Why the hell are #eng not watching the bloody ball! Get in the middle and STAY THERE! #worldcup #
  • Why are #eng letting strikers in front of them?! This is a dreadful match #worldcup #
  • Yes! Yes! Yes! #eng #worldcup #
  • That was in?!?!?!?! WHAT!?!?! #eng #worldcup #
  • That was so far in it had to go through Zim customs! FFS FIFA! #eng #worldcup #
  • #Eng should boycott ALL FIFA membership and matches until they put in goal line technology. Bastards. #
  • That's a lot of well deserved booing #eng #worldcup #
  • RT @NeilRobbins: @seppblatter you should resign. You are bad for football & your decision over goal line technology has been proven wrong #
  • That was too close for my blood pressure #eng #worldcup #
  • What the hell are #eng playing at – they can't even keep 1 player from the goal?! Fools! #worldcup #
  • #eng should be ashamed of their playing – this is not how professionals play. #worldcup #
  • #Eng aren't even trying – there was NOBODY in defense. #worldcup #
  • Much like @beautifulvenice I just caught up on #DrWho All I can say is wow – if contrived. This Doctor is a little too unbelievable. #
  • Good job I didn't go out tonight – got called out onto site to sort something out. Just think of the money.. Think of the money… #
  • RT @simon_sabin: How awesome is this http://bit.ly/czVEBL Why can't the SQL team give us great usability features like this <- IIS Express! #
  • The lesser seen sluttipuss. / @shibbygem http://twitpic.com/211iva #
  • It's not a tweetup but I'm hungry :) (@ Burger King) http://4sq.com/cYHkXn #
  • RT @TheEscapistMag Rumor: Johnny Depp Starring in Doctor Who Movie http://bit.ly/c0GBwh <- Erm.. @shibbygem #
  • RT @shibbygem: Ladies, La Senza is currently like a sweet shop pick n mix, but ya know…with knickers. £1 a pair and only fat bum sizes … #
  • RT @shanselman: LOL RT @InnyVinny start your day off right: http://img13.imageshack.us/img13/8619/2204355.swf #
  • TV business kisses HDMI goodbye – http://bit.ly/cGxWjy @THINQtech #
  • W/ @carocat @saqibs (@ Giraffe Reading) http://4sq.com/5OfoyL #
  • Excellent meal at @giraffetweet #rdg with @saqibs and @carocat. Staff amazing as always! #
  • RT @jephjacques Thanks to computer modeling, scientists have determined that giraffes "could probably swim if they wanted to." @giraffetweet #
  • Plans for a BBQ at my house tomorrow if anyone is interested? @carocat @lukesmith @jhomerston @amykate @fringley etc BYOB/food after 12pm :) #

Powered by Twitter Tools

Jul

1

The FizzBuzz problem

By pcurd

Amy Kimber posted an article today referring to a post from Nick Telford on twitter:

Will The Real Programmers Please Stand Up? http://retwt.me/1NEBw // another example of how diluted our industry really is (link)

This interests me as I often find job interviews to be very generic and after about four in a row you get to know all the answers and appear much cleverer than you perhaps are.  I like a problem that makes you think and although FizzBuzz is a trivial problem, I wanted to answer it with a little bit of real world thinking.

To quote Amy, the FizzBuzz problem is “The idea is simple, all you have to do is write a program that prints out the numbers 1 to 100, but for multiples of 3, print Fizz instead of the number and for multiples of 5 print Buzz. If the number is a multiple of both, FizzBuzz should be printed.”

In the real world, specs change. Today it’s 3 and 5 and tomorrow it’s 4 and 9 and we need to add an extra one “Bibble” for 11.  I decided to solve the problem with a list of number and word pairs so that changing it would be easy.  .Net provides a nice KeyValuePair generic which I used as Int,String.

Originally I had this implemented using a Dictionary but as FizzBuzz must be implemented the correct way around (not BuzzFizz) I needed to change this to a SortedDictionary to ensure order of execution.

Code is as below:

using System;
using System.Collections.Generic;

namespace FizzBuzz
{
    class Program
    {
        static ICollection<KeyValuePair<int, string>> wordlist;
        static void Main(string[] args)
        {
            wordlist = new SortedDictionary<int, string>();
            wordlist.Add(new KeyValuePair<int, string>(3, "Fizz"));
            wordlist.Add(new KeyValuePair<int, string>(5, "Buzz"));
            PrintFizzBuzz(1, 100);
            Console.ReadKey();
        }

        static void PrintFizzBuzz(int start, int finish)
        {
            for (int i = start; i <= finish; i++)
            {
                Console.WriteLine(CalculateFizzBuzz(i));
            }
        }

        static object CalculateFizzBuzz(int i)
        {
            string CalculateFizzBuzz = "";
            foreach (KeyValuePair<int, string> word in wordlist)
            {
                if (i % word.Key == 0)
                    CalculateFizzBuzz += word.Value;
            }
            if (CalculateFizzBuzz.Length == 0)
                CalculateFizzBuzz = i.ToString();

            return CalculateFizzBuzz;
        }
    }
}

Update 27th February 2012:

Many great answers in the comments below, great to see how different languages can solve this problem. My solution is designed around flexibility and the ability to change the list of “Fizz”es and “Buzz”es which takes away from the brevity possible!

I found this article today from Calvin Bottoms regarding implementing FizzBuzz in Haskell – at 78 characters it’s impressive and his explanation of how it develops is a great read.