Saturday, October 29, 2011

Hoppity Solution

I recently stumbled upon the facebook engineering puzzles located here and decided to try and do a few myself.  Hoppity is the first and easiest one, but I thought I'd take a stab at it here.  I'm not really sure what facebook would be looking for in order to get a job interview, but it would be have been cool to be able to view the submissions that got jobs. Please note that I didn't bother with the file reading code, but you can easily add it yourself.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HoppitySolution
{
    class Program
    {
        private const string DIV_3 = "Hoppity";
        private const string DIV_5 = "HopHop";
        private const string DIV_3_AND_5 = "Hop";

        static void Main(string[] args)
        {
            var maximumInt = GetMaximumIntFrom(args);
            Enumerable.Range(1, maximumInt).ToList().ForEach(x => ProcessHop(x));
        }

        private static void ProcessHop(int i)
        {
            if(DivisibleBy3And5(i))
            {
                Console.WriteLine(DIV_3_AND_5);
                return;
            }

            if (DivisibleBy5(i))
            {
                Console.WriteLine(DIV_5);
                return;
            }

            if (DivisibleBy3(i))
            {
                Console.WriteLine(DIV_3);
                return;
            }
        }

        private static bool DivisibleBy5(int i)
        {
            return i % 5 == 0;
        }

        private static bool DivisibleBy3(int i)
        {
            return i % 3 == 0;
        }

        private static bool DivisibleBy3And5(int i)
        {
            return DivisibleBy5(i) && DivisibleBy3(i);
        }

        private static int GetMaximumIntFrom(string[] args)
        {
            // Add File Read Code here.....
            return 15;
        }
    }
}