DEV Community

Discussion on: Daily Challenge #23 - Morse Code Decoder

Collapse
 
praneetnadkar profile image
Praneet Nadkar

Its a one liner in C# once you have the dictionary set up:

var output = input.Select(c => { c = dictionary[c]; return c; }).ToList();

Complete code:

            var dictionary = new Dictionary<string, string>
            {
                { ".-", "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"},
                { "-----", "0"},
                { ".----", "1"},
                { "..---", "2"},
                { "...--", "3"},
                { "....-", "4"},
                { ".....", "5"},
                { "-....", "6"},
                { "--...", "7"},
                { "---..", "8"},
                { "----.", "9"}
            };

            var input = Console.ReadLine().Split(' ').ToList();

            var output = input.Select(c => { c = dictionary[c]; return c; }).ToList();

            Console.WriteLine(string.Join("", output));
            Console.ReadKey();