Function to find number of customers who could not get a computer

Write a function “runCustomerSimulation” that takes following two inputs
a) An integer ‘n’: total number of computers in a cafe and a string:
b) A sequence of uppercase letters ‘seq’: Letters in the sequence occur in pairs. The first occurrence indicates the arrival of a customer; the second indicates the departure of that same customer.

A customer will be serviced if there is an unoccupied computer. No letter will occur more than two times.
Customers who leave without using a computer always depart before customers who are currently using the computers. There are at most 20 computers per cafe.

For each set of input the function should output a number telling how many customers, if any walked away without using a computer. Return 0 if all the customers were able to use a computer.

runCustomerSimulation (2, “ABBAJJKZKZ”) should return 0

runCustomerSimulation (3, “GACCBDDBAGEE”) should return 1 as ‘D’ was not able to get any computer

runCustomerSimulation (3, “GACCBGDDBAEE”) should return 0

runCustomerSimulation (1, “ABCBCA”) should return 2 as ‘B’ and ‘C’ were not able to get any computer.

runCustomerSimulation(1, “ABCBCADEED”) should return 3 as ‘B’, ‘C’ and ‘E’ were not able to get any computer.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Number of customers who walked away without using a computer = " + RunCustomerSimulation(1, "ABCBCADEED"));
        }

        public static int RunCustomerSimulation(int n, string str)
        {
            ArrayList customerList = new ArrayList();
            int currentOccupancy = 0;
            int missedCounter = 0;
            for(int i = 0; i < str.Length; i++)
            {
                //departure
                if (customerList.Contains(str[i]))
                {
                    customerList.Remove(str[i]);
                    currentOccupancy--;
                }
                //arrival
                else
                {
                    //within capacity
                    if (currentOccupancy < n)
                    {
                        customerList.Add(str[i]);
                        currentOccupancy++;
                    }
                    //beyond capacity
                    else
                    {
                        //Customers who leave without using a computer always depart before customers who are currently using the computers
                        missedCounter++;
                    }
                }
            }
            return (missedCounter/2);
        }
    }
}

Output
=======
Number of customers who walked away without using a computer = 3
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s