Created
February 15, 2014 03:34
-
-
Save jonstvns/9014201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace NumberToWords | |
{ | |
class Program | |
{ | |
private static Dictionary<int, char[]> charDict = new Dictionary<int, char[]>() { | |
{2, new char[] { 'a','b','c'}}, | |
{3, new char[] { 'd','e','f'}}, | |
{4, new char[] { 'g','h','i'}}, | |
{5, new char[] { 'j','k','l'}}, | |
{6, new char[] { 'm','n','o'}}, | |
{7, new char[] { 'p','q','r', 's'}}, | |
{8, new char[] { 't','u', 'v'}}, | |
{9, new char[] { 'w','x','y', 'z'}}, | |
}; | |
static void Main(string[] args) | |
{ | |
var wordList = ConvertToWords(23439684987); | |
for (int i = 1; i < wordList.Count; i++) | |
{ | |
Console.WriteLine(i + ": " + wordList[i]); | |
} | |
Console.WriteLine("Total Words: {0}", wordList.Count); | |
Console.WriteLine("Distinct Words: {0}", wordList.Distinct().Count()); | |
Console.ReadKey(); | |
} | |
private static List<string> ConvertToWords(long number) | |
{ | |
List<int> numList = number.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToList(); | |
var words = new List<string>(); | |
while (numList.Count > 0) | |
{ | |
words.AddRange(Permute(numList, "", 0)); | |
numList.RemoveAt(numList.Count - 1); | |
} | |
return words; | |
} | |
private static List<string> Permute(List<int> nums, string partialWord, int index) | |
{ | |
List<string> words = new List<string>(); | |
char[] chars = charDict[nums[index]]; | |
if (index < nums.Count - 1) | |
{ | |
for (int i = 0; i < chars.Length; i++) | |
{ | |
words.AddRange(Permute(nums, partialWord + chars[i], index + 1)); | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < chars.Length; i++) | |
{ | |
words.Add(partialWord + chars[i]); | |
} | |
} | |
return words; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment