codingkata.net – warm up test with fizzbuzz

First try and five minutes later I’ve got a passing solution, the Kata really makes you think about what you’re coding a bit more – I’m now hooked and I’ll be completing more Katas! You should try it at: Coding Kata.

using System;

namespace KataFizzbuzz
{
    // Any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. 
    // Numbers divisible by both become fizzbuzz. A player who makes a mistake has to take a drink. 
    // Einstein will choose a random number to start with – for example: 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz

    public class FizzBuzzKata : IFizzBuzzKata
    {
        static void Main()
        {
            var fb = new FizzBuzzKata();

            var r = new Random();

            while (true)
            {
                var seed = r.Next(0, 1000);

                Console.WriteLine("FizzBuzz number selected {0}", seed);

                var result = fb.Answer(seed);

                Console.WriteLine(result);

                Console.WriteLine("Press q to exit or any other key to play again.");

                var input = Console.ReadLine();

                if (input != null && input.Equals("q"))
                {
                    break;
                }
            }

            Console.ReadKey();
        }

        public string Answer(int i)
        {
            var result = i.ToString();

            if (i % 3 == 0)
                result = "fizz";

            if (i % 5 == 0)
                result = "buzz";

            if (i % 3 == 0 && i % 5 == 0)
                result = "fizzbuzz";

            return result;
        }
    }

    public interface IFizzBuzzKata
    {
        /// <summary>
        /// Give an answer to the current game
        /// </summary>
        /// <param name="number">current number in the game sequence</param>
        /// <returns>appropriate answer to the current number</returns>
        string Answer(int number);
    }
}

JavaScript parse query string values

A quick post on how to use JavaScript to retrieve a query string value.

   // Function to parse the querystring
   getQueryString= function(sVar) {
        return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
    };

   // Get the value
   var queryParam = getQueryString("your key");

.Net C# the default keyword

Default returns the default value when the applied type has not been initialized.

using System;

namespace CsharpDefaultDemo
{
    class CsharpDefaultExample
    {
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            var testOne = default(StringComparer);
            var testTwo = default(int);
            var testThree = default(bool);
            var testFour = default(MyClass);
            var testFive = default(long);

            Console.WriteLine(testOne);
            Console.WriteLine(testTwo);
            Console.WriteLine(testThree);
            Console.WriteLine(testFour);
            Console.WriteLine(testFive);

            Console.ReadKey();
        }
    }

    /// <summary>
    /// Simple class for demonstration
    /// </summary>
    public class MyClass
    {
    }
}

Returns:


0
False

0

Umbraco 7 first look and installation

I’ve recently been involved in a project deploying an open source .Net based CMS called Umbraco. It’s been interesting to compare it to more mature products like Microsoft SharePoint and in particular the web content management features.

First step is to install from source, this is a quick post to show the installation steps:

1) Grab the source (7.0.1) from GitHub
Um7GitHub

2) This is how the 7.0.1 source tree looks:
Um7Solution

3) Initial install screen
InstallOne

4) Accept the license
InstallTwo

5) Tell the installation about your database
InstallThree

6) Install the database
InstallFour

7) Setup login details
InstallFive

8) Starter kit screen
InstallSix

9) Install a starter kit (or not in this case)
InstallSeven

10) Complete the setup.
InstallEight

I’ll be looking more at Umbraco 7 and how the development process works, this will happen in future posts.

.Net C# dynamic typing

C# 4 introduced dynamic typing into the language specification, this give the developer the ability to declare something that can be decided at run-time rather than compile-time. One of the advantages of this feature is the ability to deal with uncertain types such as objects returned from COM. It also allows C# to emulate the behaviour of languages such as: Perl, Ruby, Python etc.

using System;

namespace CsharpDynamicDemo
{
    class CsharpDynamicExample
    {
        static void Main()
        {
            dynamic dynamicVariable = "Hello I'm dynamic.";

            const string staticVariable = "I'm static.";

            Console.WriteLine(dynamicVariable.GetType());
            
            Console.WriteLine(staticVariable.GetType());

            Console.ReadKey();
        }
    }
}

Returns:

System.String
System.String