-
-
Save Ezeji/905f53ce26f9134cadebdaa1694e20a9 to your computer and use it in GitHub Desktop.
class Program | |
{ | |
private static readonly string[] two = { "a","b","c" }; | |
private static readonly string[] three = { "d", "e", "f" }; | |
private static readonly string[] four = { "g", "h", "i" }; | |
private static readonly string[] five = { "j", "k", "l" }; | |
private static readonly string[] six = { "m", "n", "o" }; | |
private static readonly string[] seven = { "p", "q", "r", "s" }; | |
private static readonly string[] eight = { "t", "u", "v" }; | |
private static readonly string[] nine = { "w", "x", "y", "z" }; | |
static void Main(string[] args) | |
{ | |
var result = FirstPossibleCombinations(23); | |
foreach (var combination in result) | |
{ | |
Console.WriteLine("Result:" + " " + combination); | |
} | |
} | |
public static List<string> FirstPossibleCombinations(int num) | |
{ | |
var convertedNum = num.ToString(); | |
var length = IntLength(Convert.ToInt32(num)); | |
List<string> result = new List<string>(); | |
if (length > 2) return null; | |
else if(convertedNum == null) | |
{ | |
return null; | |
} | |
else | |
{ | |
if (convertedNum[0] == '2' && convertedNum[1] == '3') | |
{ | |
result.AddRange(CombinationForNumbersTwoAndThree()); | |
} | |
else if (convertedNum[0] == '3' && convertedNum[1] == '4') | |
{ | |
result.AddRange(CombinationForNumbersThreeAndFour()); | |
} | |
else if (convertedNum[0] == '4' && convertedNum[1] == '5') | |
{ | |
result.AddRange(CombinationForNumbersFourAndFive()); | |
} | |
else if (convertedNum[0] == '5' && convertedNum[1] == '6') | |
{ | |
result.AddRange(CombinationForNumbersFiveAndSix()); | |
} | |
else if (convertedNum[0] == '6' && convertedNum[1] == '7') | |
{ | |
result.AddRange(CombinationForNumbersSixAndSeven()); | |
} | |
else if (convertedNum[0] == '7' && convertedNum[1] == '8') | |
{ | |
result.AddRange(CombinationForNumbersSevenAndEight()); | |
} | |
else if (convertedNum[0] == '8' && convertedNum[1] == '9') | |
{ | |
result.AddRange(CombinationForNumbersEightAndNine()); | |
} | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersTwoAndThree() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < three.Length; i++) | |
{ | |
result.Add(two[0] + three[i]); | |
} | |
for (int i = 0; i < three.Length; i++) | |
{ | |
result.Add(two[1] + three[i]); | |
} | |
for (int i = 0; i < three.Length; i++) | |
{ | |
result.Add(two[2] + three[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersThreeAndFour() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < four.Length; i++) | |
{ | |
result.Add(three[0] + four[i]); | |
} | |
for (int i = 0; i < four.Length; i++) | |
{ | |
result.Add(three[1] + four[i]); | |
} | |
for (int i = 0; i < four.Length; i++) | |
{ | |
result.Add(three[2] + four[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersFourAndFive() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < five.Length; i++) | |
{ | |
result.Add(four[0] + five[i]); | |
} | |
for (int i = 0; i < five.Length; i++) | |
{ | |
result.Add(four[1] + five[i]); | |
} | |
for (int i = 0; i < five.Length; i++) | |
{ | |
result.Add(four[2] + five[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersFiveAndSix() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < six.Length; i++) | |
{ | |
result.Add(five[0] + six[i]); | |
} | |
for (int i = 0; i < six.Length; i++) | |
{ | |
result.Add(five[1] + six[i]); | |
} | |
for (int i = 0; i < six.Length; i++) | |
{ | |
result.Add(five[2] + six[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersSixAndSeven() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < seven.Length; i++) | |
{ | |
result.Add(six[0] + seven[i]); | |
} | |
for (int i = 0; i < seven.Length; i++) | |
{ | |
result.Add(six[1] + seven[i]); | |
} | |
for (int i = 0; i < seven.Length; i++) | |
{ | |
result.Add(six[2] + seven[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersSevenAndEight() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < eight.Length; i++) | |
{ | |
result.Add(seven[0] + eight[i]); | |
} | |
for (int i = 0; i < eight.Length; i++) | |
{ | |
result.Add(seven[1] + eight[i]); | |
} | |
for (int i = 0; i < eight.Length; i++) | |
{ | |
result.Add(seven[2] + eight[i]); | |
} | |
return result; | |
} | |
public static List<string> CombinationForNumbersEightAndNine() | |
{ | |
List<string> result = new List<string>(); | |
for (int i = 0; i < nine.Length; i++) | |
{ | |
result.Add(eight[0] + nine[i]); | |
} | |
for (int i = 0; i < nine.Length; i++) | |
{ | |
result.Add(eight[1] + nine[i]); | |
} | |
for (int i = 0; i < nine.Length; i++) | |
{ | |
result.Add(eight[2] + nine[i]); | |
} | |
return result; | |
} | |
public static int IntLength(int i) | |
{ | |
if (i < 0) | |
throw new ArgumentOutOfRangeException(); | |
if (i == 0) | |
return 1; | |
return (int)Math.Floor(Math.Log10(i)) + 1; | |
} | |
} |
Hello @meekg33k
Thank you for reaching out. I have been expecting your feedback really 😅
So I couldn't solve for more than a two test case and wanted to attempt the problem however. I'd appreciate I get a detailed correction or feedback on how I could solve such a problem. If possible, recommended article(s) will be of great help. I would also appreciate recommended resources to help me learn Algorithm and Data Structures. Thanks in advance.
Great hearing from you @Ezeji, one way for solving this kind of problem would be using recursion where you start to solve the problem starting with base case.
For learning data structures and algorithms in general, https://www.geeksforgeeks.org/ is generally a good resource.
Thank you so much @meekg33k for your feedback and the resource shared.
I'd sure check it out and hope to strengthen my learning. Also, I would reach out to you from time to time when I have blockers.
Thanks once again.
Hello @Ezeji, thank you for participating in Week 8 of #AlgorithmFridays.
Your solution works for the test case where the input is 2 digits; for example
"23"
but doesn't pass when the input is more than 2 digits. Did you have a chance to test your solution with inputs with more than 2 digits?Looking forward to hearing your thoughts.