arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL Server 2008/2012/2014/2016/CE An Open Source C# web crawler with Lucene.NET search using MongoDB/RavenDB/Hadoop

Completely Open Source @ GitHub

Does arachnode.net scale? | Download the latest release
Programming Challenge 1

Question: A solution consists of four balls from a set of four different colors. The user tries to guess the solution.

If they guess the right color for the right spot, record it as being in the correct 'Location'. If it's the right color, but the wrong spot, record it as a correct 'Color'. For example: if the solution is 'BGRR' and the user guesses 'RGYY' they have 1 'Location' and 1 'Color'. A correct solution would be 4 'Location' and 0 'Color'.

Write a program to, given a solution and a guess, calculate the response that we present to the user.

Answer:

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

namespace Interview
{
    class Program
    {
        static void Main(string[] args)
        {
            int isLocationCorrectCount = 0;
            int isColorCorrectCount = 0;

            char[] solution = GetBalls();

            for (int i = 0; i < 4; i++)
            {
                Console.Write(solution[ i ]);
            }

            while (isLocationCorrectCount != 4)
            {
                Console.WriteLine("\nTake a guess?\n");

                bool isRedColorCorrect = false;
                bool isGreenColorCorrect = false;
                bool isBlueColorCorrect = false;
                bool isYellowColorCorrect = false;

                isLocationCorrectCount = 0;
                isColorCorrectCount = 0;

                try
                {
                    char[] guess = Console.ReadLine().ToUpper().ToCharArray();

                    if (guess.Length != 4)
                    {
                        Console.WriteLine("\nGuess 4 please!");

                        continue;
                    }

                    for (int i = 0; i < guess.Length; i++)
                    {
                        //check for location...
                        if (guess[ i ] == solution[ i ])
                        {
                            isLocationCorrectCount++;
                        }
                        else
                        {
                            //check for the color elsewhere...
                            for (int j = 0; j < guess.Length; j++)
                            {
                                if (i != j)
                                {
                                    if (guess[ i ] == solution[j])
                                    {
                                        //only record the color correct per color...
                                        switch (guess[ i ])
                                        {
                                            case 'R':
                                                if (!isRedColorCorrect)
                                                {
                                                    isRedColorCorrect = true;
                                                    isColorCorrectCount++;
                                                }
                                                break;
                                            case 'G':
                                                if (!isGreenColorCorrect)
                                                {
                                                    isGreenColorCorrect = true;
                                                    isColorCorrectCount++;
                                                }
                                                break;
                                            case 'B':
                                                if (!isBlueColorCorrect)
                                                {
                                                    isBlueColorCorrect = true;
                                                    isColorCorrectCount++;
                                                }
                                                break;
                                            case 'Y':
                                                if (!isYellowColorCorrect)
                                                {
                                                    isYellowColorCorrect = true;
                                                    isColorCorrectCount++;
                                                }
                                                break;
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                    }

                    Console.WriteLine("Location: " + isLocationCorrectCount + " & Color: " + isColorCorrectCount);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }

            Console.WriteLine("You win!");

            Console.ReadLine();
        }

        static char[] GetBalls()
        {
            Random _random = new Random();

            char[] balls = new char[4];

            for(int i = 0; i < 4; i++)
            {
                int ballColor = _random.Next(4);
                
                switch(ballColor)
                {
                    case 0:
                        balls[ i ] = 'R';
                        break;
                    case 1:
                        balls[ i ] = 'G';
                        break;
                    case 2:
                        balls[ i ] = 'B';
                        break;
                    case 3:
                        balls[ i ] = 'Y';
                        break;
                }
            }

            return balls;
        }
    }
}

Posted Thu, Feb 7 2013 10:27 AM by arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL 2008/2012/CE

copyright 2004-2017, arachnode.net LLC