October 31, 2013

This is what happens when Google and Pixar work together

http://www.wired.com/business/2013/10/motorola-google-mouse/

It's.. I don't even know.  I read about it before I saw it in person today.  The descriptions don't do it justice.  I was shrieking with joy as I panned my friend's phone across all 360 degrees and watched the virtual reality of a mouse losing his hat unfold.


October 25, 2013

Ice Cream Delegates

Because programming concepts are always so much more delicious when you add ice cream to them.

delegate string IceCreamShop(string iceCreamFlavor);

static string Jenis(string iceCreamFlavor)
{
    string betterFlavor = "no jenis for you";

    switch (iceCreamFlavor.ToLower()) {
        case "chocolate":
            betterFlavor = "Askinosie Dark Chocolate";
            break;
        case "vanilla":
            betterFlavor = "Ugandan Vanilla Bean";
            break;
        case "peanut butter":
            betterFlavor = "Buckeye State";
            break;
    }

    return betterFlavor;
}

static string CheapIceCreamStore(string iceCreamFlavor)
{
    string ickyFlavor = "no cheap ice cream for you but that's probably not a bad thing";

    switch (iceCreamFlavor.ToLower())
    {
        case "chocolate":
            ickyFlavor = "mysterious brown stuff";
            break;
        case "vanilla":
            ickyFlavor = "mayonnaise";
            break;
        case "peanut butter":
            ickyFlavor = "dirt";
            break;
    }

    return ickyFlavor;
}

And this is how you use it:

static void Main(string[] args)
{
    IceCreamShop iceCreamShop = Jenis;
    Console.WriteLine(iceCreamShop("chocolate"));

    iceCreamShop = CheapIceCreamStore;
    Console.WriteLine(iceCreamShop("chocolate"));
}

The output is, predictably:

Askinosie Dark Chocolate
mysterious brown stuff

I can also pass in my ice cream shop delegate as a parameter!

static void PrintMenu(List<string> flavors, IceCreamShop iceCreamShop)
{
    foreach (string flavor in flavors)
    {
        Console.WriteLine(iceCreamShop(flavor));
    }
}

static void Main(string[] args)
{
    var flavors = new List<string> { "chocolate", "vanilla", "peanut butter"};

    IceCreamShop iceCreamShop = Jenis;
    Console.WriteLine("Delicious Jeni's menu:");
    PrintMenu(flavors, iceCreamShop);

    Console.WriteLine();

    iceCreamShop = CheapIceCreamStore;
    Console.WriteLine("icky other ice cream store menu:");
    PrintMenu(flavors, iceCreamShop);
}

With the output:

Delicious Jeni's menu:
Askinosie Dark Chocolate
Ugandan Vanilla Bean
Buckeye State

icky other ice cream store menu:
mysterious brown stuff
mayonnaise
dirt

Columbus natives will appreciate the Jeni's reference. If you don't know what Jeni's is, I'm sorry to know that but it's such a good ice cream store that it's spreading across the country and you will hopefully soon know what I am talking about.  I recommend Salty Caramel to all Jeni's first timers.

Cool websites

http://www.lifehack.org/articles/technology/25-killer-websites-that-make-you-cleverer-2.html

October 23, 2013

Interview Question - Get Permutations of a String

My algorithm follows the same idea as Gayle's in Cracking the Coding Interview, I've just implemented it iteratively instead of recursively

public static List<string> GetPermutations(string input)
{
   var permutes = new List<string>();
   for (int i = 0; i < input.Length; i++)
   {
      if (i == 0)
      {
         permutes.Add(input[i].ToString());
      }
      else
      {
          var newPermutes = new List<string>();
          foreach (string word in permutes)
          {
             AddCharToEveryPosition(input[i], word, newPermutes);
          }
          permutes = newPermutes;
      }
  }
  return permutes;
}

private static void AddCharToEveryPosition(char c, string word, List<string> newPermutes)
{
   for (int i = 0; i <= word.Length; i++)
   {
      string newWord = word.Insert(i, c.ToString());
      newPermutes.Add(newWord);
   }
}

I'll run through the algorithm a bit since it helps me to understand what's going on.

Say input is "meh"
We take the first letter 'm'
Then we take the second letter 'e' and add it wherever we can to the first letter.  For now we can't go too crazy, we just get { "em", "me" }
Third letter is slightly more interesting, since we now get { "hem", "ehm", "emh", "hme", "mhe", "meh" }
You can imagine how crazy it gets with more letters.

Yay permutations!

Interview Question - Add two integers without using + sign

This seems simple enough but to those not too comfortable with bits, it's a bit tricky (ha...)

public int AddWithBits(int a, int b) {
   while (b > 0) {
      var sum = a ^ b;
      var carry = (a & b) << 1;
      a = sum;
      b = carry;
   }

   return a;
}