Hi

The following C# code matched the expected output when giving it the supplied 
test inputs. But when I submitted I got a RE (runtime error) and cannot 
understand why. Any suggestions?

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

namespace Cryptopangrams 
{
   
    class Program
    {
        static List<string> parameters;
        static List<string> testCases;
        static string numTestCases;
        const char East = 'E';
        const char South = 'S';

        static List<int> intAlpha;
        static List<int> primeAlpha;
        static List<int> sortedPrimeList;

        static List<int> primes;

        static char[] alphabet = new char[]
        {
            
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        };

        static void Main(string[] args)
        {
            parameters = new List<string>();
            testCases = new List<string>();

            GetStandardInput();

            var testCaseNumber = 1;



            for (int i = 0; i < Convert.ToInt32(numTestCases); i++)
            {
                primeAlpha = new List<int>();
                primes = new List<int>();
                sortedPrimeList = new List<int>();
                intAlpha = new List<int>();

                var param = parameters[i].Split(' ');
                var PrimeLimit = param[0];
                var Num = param[1];

                var cryptList = new List<string>(testCases[i].Split(' '));

                GetPrimes(Convert.ToInt32(PrimeLimit));

                var cryptIntList = ConvertToInt(cryptList);

                Decypher(cryptIntList);

                var distinctList = GetAlphabet();

                var output = getOutput(distinctList, cryptIntList);

                Console.WriteLine("Case #{0}: {1}", testCaseNumber, new 
string(output));

                testCaseNumber++;
            }
        }

        private static char[] getOutput(List<int> distinctList, List<int> 
cryptIntList)
        {
            char[] outputText = new char[primeAlpha.Count];

            ///get primeVal,
            ///find letter and substitue.
            ///
            for (int i = 0; i < primeAlpha.Count; i++)
            {
                var prime = primeAlpha[i];

                var location = distinctList.IndexOf(prime);

                var letter = alphabet[location];

                outputText[i] = letter;
            }

            return outputText;
        }

        private static List<int> GetAlphabet()
        {
            for (int i = 0; i < primeAlpha.Count; i++)
            {
                sortedPrimeList.Add(primeAlpha[i]);
            }

            sortedPrimeList.Sort();

            var distinctList = sortedPrimeList.Distinct().ToList();

            return distinctList;
        }

        private static List<int> ConvertToInt(List<string> cryptList)
        {
            var intList = new List<int>();

            foreach (var crypt in cryptList)
            {
                intList.Add(Convert.ToInt32(crypt));
            }

            return intList;
        }

        private static void Decypher(List<int> cryptList)
        {
            //var first = cryptList.First();

            //var candidatesFirst = new List<int>();

            //for (int i = 0; i < primes.Count; i++)
            //{
            //    if (first % primes[i] == 0)
            //    {
            //        candidatesFirst.Add(primes[i]);
            //    }
            //}

            var candidates = new List<int>()
            {
                0,0
            };
            var prevCandidates = new List<int>()
            {
                0,0
            };

            for (int i = 0; i < cryptList.Count; i++)
            {
                int jj = 0;

                if (candidates.Count > 0)
                {
                    prevCandidates[0] = candidates[0];
                    prevCandidates[1] = candidates[1];
                }

                for (int j = 0; j < primes.Count; j++)
                {
                    if (cryptList[i] % primes[j] == 0)
                    {
                        candidates[jj] = primes[j];
                        jj++;
                    }
                }



                //first case

                // thereafter
                var value1 = candidates.IndexOf(prevCandidates[0]);
                var value2 = candidates.IndexOf(prevCandidates[1]);

                var toAdd = value1 >= 0 ? prevCandidates[0] : prevCandidates[1];

                if (i == 1)
                {
                    var firstVal = value1 >= 0 ? prevCandidates[1] : 
prevCandidates[0];
                    primeAlpha.Add(firstVal);
                    intAlpha.Add(cryptList[0]);
                }

                if (toAdd > 0)
                {
                    primeAlpha.Add(toAdd);
                    intAlpha.Add(cryptList[i]);
                }

                if (i == (cryptList.Count - 1))
                {
                    var lastVal = (toAdd == candidates[0]) ? candidates[1] : 
candidates[0];
                    primeAlpha.Add(lastVal);
                }


            }


            // go through array of primes
            // divide by prime and see if mod is 0
            // get two results
            // ab or ba
            // divide next
            // then now if ab or ba and proceed

            // if number in array then dont calculae
            //Stop once populated

            // then sort and work out alphabet
        }

        private static void GetStandardInput()
        {
            numTestCases = Console.ReadLine();
            var num = Convert.ToInt32(numTestCases);

            for (int i = 0; i < num; i++)
            {
                parameters.Add(Console.ReadLine());
                testCases.Add(Console.ReadLine());
            }
        }

        private static void GetPrimes(int limit)
        {
            bool isPrime = true;

            for (int i = 2; i <= limit; i++)
            {
                isPrime = true;
                var start = (i / 2);

                for (int j = start; j > 1; j--)
                {
                    if (i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }

                if (isPrime)
                {
                    primes.Add(i);
                }
            }
        }
    }   
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/7b46e71b-a4f2-411d-bcbc-bd4689eb9b74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to