How can I print out all possible letter combinations a given phone number can represent?

前端 未结 30 2191
逝去的感伤
逝去的感伤 2020-12-22 18:01

I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations

30条回答
  •  失恋的感觉
    2020-12-22 18:43

    namespace WordsFromPhoneNumber
    {
        /// 
        /// Summary description for WordsFromPhoneNumber
        /// 
        [TestClass]
        public class WordsFromPhoneNumber
        {
            private static string[] Chars = { "0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ" };
            public WordsFromPhoneNumber()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            #region overhead
    
            private TestContext testContextInstance;
    
            /// 
            ///Gets or sets the test context which provides
            ///information about and functionality for the current test run.
            ///
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
    
            #region Additional test attributes
            //
            // You can use the following additional attributes as you write your tests:
            //
            // Use ClassInitialize to run code before running the first test in the class
            // [ClassInitialize()]
            // public static void MyClassInitialize(TestContext testContext) { }
            //
            // Use ClassCleanup to run code after all tests in a class have run
            // [ClassCleanup()]
            // public static void MyClassCleanup() { }
            //
            // Use TestInitialize to run code before running each test 
            // [TestInitialize()]
            // public void MyTestInitialize() { }
            //
            // Use TestCleanup to run code after each test has run
            // [TestCleanup()]
            // public void MyTestCleanup() { }
            //
            #endregion
    
            [TestMethod]
            public void TestMethod1()
            {
                IList words = Words(new int[] { 2 });
                Assert.IsNotNull(words, "null");
                Assert.IsTrue(words.Count == 3, "count");
                Assert.IsTrue(words[0] == "A", "a");
                Assert.IsTrue(words[1] == "B", "b");
                Assert.IsTrue(words[2] == "C", "c");
            }
    
            [TestMethod]
            public void TestMethod23()
            {
                IList words = Words(new int[] { 2 , 3});
                Assert.IsNotNull(words, "null");
                Assert.AreEqual(words.Count , 9, "count");
                Assert.AreEqual(words[0] , "AD", "AD");
                Assert.AreEqual(words[1] , "AE", "AE");
                Assert.AreEqual(words[2] , "AF", "AF");
                Assert.AreEqual(words[3] , "BD", "BD");
                Assert.AreEqual(words[4] , "BE", "BE");
                Assert.AreEqual(words[5] , "BF", "BF");
                Assert.AreEqual(words[6] , "CD", "CD");
                Assert.AreEqual(words[7] , "CE", "CE");
                Assert.AreEqual(words[8] , "CF", "CF");
            }
    
            [TestMethod]
            public void TestAll()
            {
                int[] number = new int [4];
                Generate(number, 0);
            }
    
            private void Generate(int[] number, int index)
            {
                for (int x = 0; x <= 9; x += 3)
                {
                    number[index] = x;
                    if (index == number.Length - 1)
                    {
                        var w = Words(number);
                        Assert.IsNotNull(w);
                        foreach (var xx in number)
                        {
                            Console.Write(xx.ToString());
                        }
                        Console.WriteLine(" possible words:\n");
                        foreach (var ww in w)
                        {
                            Console.Write("{0} ", ww);
                        }
                        Console.WriteLine("\n\n\n");
                    }
                    else
                    {
                        Generate(number, index + 1);
                    }
                }
            }
    
            #endregion
    
            private IList Words(int[] number)
            {
                List words = new List(100);
                Assert.IsNotNull(number, "null");
                Assert.IsTrue(number.Length > 0, "length");
                StringBuilder word = new StringBuilder(number.Length);
                AddWords(number, 0, word, words);
    
                return words;
            }
    
            private void AddWords(int[] number, int index, StringBuilder word, List words)
            {
                Assert.IsTrue(index < number.Length, "index < length");
                Assert.IsTrue(number[index] >= 0, "number >= 0");
                Assert.IsTrue(number[index] <= 9, "number <= 9");
    
                foreach (var c in Chars[number[index]].ToCharArray())
                {
                    word.Append(c);
                    if (index < number.Length - 1)
                    {
                        AddWords(number, index + 1, word, words);
                    }
                    else
                    {
                        words.Add(word.ToString());
                    }
                    word.Length = word.Length - 1;
                }
            }
        }
    }
    

提交回复
热议问题