Example #1
0
            /*
             * @number - a string representing an integer from 1 to 1000,
             */
            public static HashedInteger Convert(string number)
            {
                HashedInteger digits = new HashedInteger();

                foreach (char c in number)
                {
                    digits.values[c - '0'] = 1;
                }

                return(digits);
            }
Example #2
0
            /*
             * Find largest digit in two numbers expressed in Digits
             *
             * return: last digit
             */
            public static int GetLastDigit(HashedInteger firstOne, HashedInteger secondOne)
            {
                int lastDigit = 0;

                for (int i = 9; i >= 0; i--)
                {
                    if (firstOne.values[i] == 1 && secondOne.values[i] == 1)
                    {
                        lastDigit = i;
                        break;
                    }
                }

                return(lastDigit);
            }
Example #3
0
            public static HashedInteger[] ConvertAll(string[] numbers)
            {
                if (numbers == null || numbers.Length == 0)
                {
                    return new HashedInteger[] { }
                }
                ;

                int length = numbers.Length;
                IList <HashedInteger> output = new List <HashedInteger>();

                foreach (string number in numbers)
                {
                    output.Add(HashedInteger.Convert(number));
                }

                return(output.ToArray());
            }
Example #4
0
        /*
         *
         * Choose any number from each basket, compare two numbers to find largest digit in both numbers.
         * If the largest digit is the even number, then add to the sum. Ruturn the value of sum of largest digit
         * of a pair number from two baskets.
         */
        private static int CalculateSumOfEvenNumbers(string[] numbersA, string[] numbersB)
        {
            int count = 0;

            HashedInteger[] shortNumbers1 = HashedInteger.ConvertAll(numbersA);
            HashedInteger[] shortNumbers2 = HashedInteger.ConvertAll(numbersB);

            foreach (HashedInteger firstOne in shortNumbers1)
            {
                foreach (HashedInteger secondOne in shortNumbers2)
                {
                    int lastDigit = HashedInteger.GetLastDigit(firstOne, secondOne);

                    if (lastDigit % 2 == 0)
                    {
                        count++;
                    }
                }
            }

            return(count);
        }