On the 14th September 2013 I was participating in a C# competition for advanced by Telerik Academy. After spending 5 hours, trying to solve 5 quite tough C# problems, I have obtained 140 points. With this result I was ranked in 140 position. Being in top 140 is probably not something man should be very proud of, but considering the fact that it was enough to continue I was quite satisfied. Furthermore, 6 months ago 600 people were accepted in the courses and other 200 were participating from previous years.
So, the first problem was really easy and I needed only about 30 minutes to obtain the maximum of 100 points. I was a little mad on me later, when I have obtained only 40 points more for about 4:30 hours, but the problems were really tough and I have not studied enough for the competititon.
Anyway, if you are interested in the task, I was able to resolve in 30 minutes, you may find the complete description of the problem here.
With simple words, what is required is to write a C# console application, which recognizes thirteen different 3 CHAR numbers and converts them to standard 10 digit numbers. For example, if you enter in the console “CHU” it will return “0” and if you enter “TELOFT” it will return 15. Pretty much it is like the known conversion of 16 digit system to 10 digit system, but in this case we have 13 digit system and each digits consist of three chars.
This is the code, which was provided by Telerik, as a sample of good solution to the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
namespace MultiverseCommunication { using System; public class Multiverse { // converts number from "multiverse" number to normal 13-th based number public static long ConvertMultiverseNumber(string input) { long result = 0; for (int i = 0, j = input.Length / 3 - 1; i < input.Length; i += 3, j--) { int currentDigit = ConvertDigitFromMultiverse(input.Substring(i, 3)); result += currentDigit * PowerOfThirdteen(j); } return result; } // converts from "multiverse" digit to normal 13-th based digit public static int ConvertDigitFromMultiverse(string multiverseDigit) { switch (multiverseDigit) { case "CHU": return 0; case "TEL": return 1; case "OFT": return 2; case "IVA": return 3; case "EMY": return 4; case "VNB": return 5; case "POQ": return 6; case "ERI": return 7; case "CAD": return 8; case "K-A": return 9; case "IIA": return 10; case "YLO": return 11; case "PLA": return 12; default: throw new ArgumentException(); } } // get the power of 13 public static long PowerOfThirdteen(int power) { long result = 1; for (int i = 0; i < power; i++) { result *= 13; } return result; } public static void Main() { Console.WriteLine(ConvertMultiverseNumber(Console.ReadLine())); } } } |
I have not saved my code, but it was something similar as far as it has passed all the tests.
What is interesting in this code:
1. We use 3 additional methods to resolve the problem – Power of Thirteen, ConvertDigitFromMultiverse and ConvertMultiverseNumber.
2. The main method consists of only one line – writeline with a method inside. Really beautiful 🙂
3. The problem that the digits from which we should convert are actually three symbols, is beautifully resolved with just 3 lines of code:
1 2 3 4 5 6 |
for (int i = 0, j = input.Length / 3 - 1; i < input.Length; i += 3, j--) { int currentDigit = ConvertDigitFromMultiverse(input.Substring(i, 3)); result += currentDigit * PowerOfThirdteen(j); } |
Pretty much that was it. It may seem a standard C# problem, but for me it was something to be proud of 🙂
Here is the *.exe file, which does the conversion. So, if you ever have to transfer from any kind of digit system to the standard 10 digit system, with a little amendment in this file, you may do it.
Enjoy!