C
C#2y ago
احمد

✅ Morse Code Decoder

using System.Collections.Generic;


class MorseCodeDecoder
{
public static string Decode(string morseCode)
{

string[] morse_code_holder = morseCode.Split(' ');

Dictionary<char, string> _morseCode = new Dictionary<char, 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', "----."},
{'.', ".-.-.-"},
{',', "--..--"},
{'?', "..--.."},
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
};

foreach ( var kvp in _morseCode )
{
char kvpk = kvp.Key;
int i = 0;

if (morse_code_holder[i].Contains(kvpk))
{
morse_code_holder[i] = kvp.Value;
}
}

string sumOfMorseCode = string.Join(" ", morse_code_holder);

Console.WriteLine(sumOfMorseCode);

throw new System.NotImplementedException("Please provide some code.");
}
}
using System.Collections.Generic;


class MorseCodeDecoder
{
public static string Decode(string morseCode)
{

string[] morse_code_holder = morseCode.Split(' ');

Dictionary<char, string> _morseCode = new Dictionary<char, 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', "----."},
{'.', ".-.-.-"},
{',', "--..--"},
{'?', "..--.."},
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
};

foreach ( var kvp in _morseCode )
{
char kvpk = kvp.Key;
int i = 0;

if (morse_code_holder[i].Contains(kvpk))
{
morse_code_holder[i] = kvp.Value;
}
}

string sumOfMorseCode = string.Join(" ", morse_code_holder);

Console.WriteLine(sumOfMorseCode);

throw new System.NotImplementedException("Please provide some code.");
}
}
16 Replies
احمد
احمدOP2y ago
i don't understand what i'm doing wrong, any assisstance?
many things
many things2y ago
are you rewriting morse_code_holder instead of creating a new list? also you are cycling the dictionary instead of the string you have to convert...? (i will admit better variable naming would help)
احمد
احمدOP2y ago
hold on
many things
many things2y ago
i believe there is also another logic error i'm opening visual studio
احمد
احمدOP2y ago
uh oh
using System.Collections.Generic;
using System.Text;



class MorseCodeDecoder
{
public static string Decode(string morseCode)
{

string[] morse_code_holder = morseCode.Split(" ");

Dictionary<char, string> _morseCode = new Dictionary<char, 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', "----."},
{'.', ".-.-.-"},
{',', "--..--"},
{'?', "..--.."},
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
};

StringBuilder translatedString = new StringBuilder();

foreach (var morseCodeString in morse_code_holder)
{
if (_morseCode.ContainsValue(morseCodeString))
{
char char_replacement = _morseCode.FirstOrDefault(x => x.Value == morseCodeString).Key;
translatedString.Append(char_replacement);
}
}

string result = translatedString.ToString();

Console.WriteLine(result);

return result;

throw new System.NotImplementedException("Please provide some code.");
}
}
using System.Collections.Generic;
using System.Text;



class MorseCodeDecoder
{
public static string Decode(string morseCode)
{

string[] morse_code_holder = morseCode.Split(" ");

Dictionary<char, string> _morseCode = new Dictionary<char, 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', "----."},
{'.', ".-.-.-"},
{',', "--..--"},
{'?', "..--.."},
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
};

StringBuilder translatedString = new StringBuilder();

foreach (var morseCodeString in morse_code_holder)
{
if (_morseCode.ContainsValue(morseCodeString))
{
char char_replacement = _morseCode.FirstOrDefault(x => x.Value == morseCodeString).Key;
translatedString.Append(char_replacement);
}
}

string result = translatedString.ToString();

Console.WriteLine(result);

return result;

throw new System.NotImplementedException("Please provide some code.");
}
}
still doesn't work rip in chat
many things
many things2y ago
this should be alright
public string Encode(string input)
{
Dictionary<char, string> _charToMorseCode = new()
{
{'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 decoded = input.Select(c => _charToMorseCode.GetValueOrDefault(char.ToUpper(c)));
return string.Join(" ", decoded);
public string Encode(string input)
{
Dictionary<char, string> _charToMorseCode = new()
{
{'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 decoded = input.Select(c => _charToMorseCode.GetValueOrDefault(char.ToUpper(c)));
return string.Join(" ", decoded);
don't you see a problem here?
string[] morse_code_holder = morseCode.Split(" ");
how is your input
احمد
احمدOP2y ago
No What’s wrong with it String[] A list of strings What’s wrong with that Turns like —- -.- into
“—-“ and “-.-“
“—-“ and “-.-“
many things
many things2y ago
ok i'll take another hint what about this
string[] morse_code_holder = morseCode.Split(" ");
string[] morse_code_holder = morseCode.Split(" ");
and this
new Dictionary {
{ entries... },
{' ', "/"} // <---
};
new Dictionary {
{ entries... },
{' ', "/"} // <---
};
احمد
احمدOP2y ago
what am i meant to do with that oh characters i should've made it " "?
{" ", "/'"}
{" ", "/'"}
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
{'\'', ".----."},
{'!', "-.-.--"},
{'/', "-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'&', ".-..."},
{':', "---..."},
{';', "-.-.-."},
{'=', "-...-"},
{'+', ".-.-."},
{'-', "-....-"},
{'_', "..--.-"},
{'\"', ".-..-."},
{'$', "...-..-"},
{'@', ".--.-."},
{' ', "/"}
many things
many things2y ago
what is happening there you are splitting input by ' ' and then you have a match in the decoding dictionary for ' '
احمد
احمدOP2y ago
oh uh so what do i split by? ''?
many things
many things2y ago
why would you split the input to begin with? wait, is the direction morse to char or char to morse because a Decode method would make me think of morse to text
احمد
احمدOP2y ago
ima js leave this flipping question it' s so annoying ima move onto another test
many things
many things2y ago
i would it like this
(char TextChar, string MorseCode)[] _morseCode = new[]
{
('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', "----."),
('.', ".-.-.-"), (',', "--..--"), ('?', "..--.."), ('\'', ".----."), ('!', "-.-.--"), ('/', "-..-."),
('(', "-.--."), (')', "-.--.-"), ('&', ".-..."), (':', "---..."), (';', "-.-.-."), ('=', "-...-"),
('+', ".-.-."), ('-', "-....-"), ('_', "..--.-"), ('\"', ".-..-."), ('$', "...-..-"), ('@', ".--.-."),
(' ', "/")
};

public string Decode(string input)
{
var morseCodeToChar = _morseCode.ToDictionary(_ => _.MorseCode, _ => _.TextChar);
var decoded = input.Split(' ').Select(morseCodeToChar.GetValueOrDefault);
return string.Concat(decoded);
}

public string Encode(string morseInput)
{
var charToMorseCode = _morseCode.ToDictionary(_ => _.TextChar, _ => _.MorseCode);
var decoded = morseInput.Select(c => charToMorseCode.GetValueOrDefault(char.ToUpper(c)));
return string.Join(" ", decoded);
}
(char TextChar, string MorseCode)[] _morseCode = new[]
{
('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', "----."),
('.', ".-.-.-"), (',', "--..--"), ('?', "..--.."), ('\'', ".----."), ('!', "-.-.--"), ('/', "-..-."),
('(', "-.--."), (')', "-.--.-"), ('&', ".-..."), (':', "---..."), (';', "-.-.-."), ('=', "-...-"),
('+', ".-.-."), ('-', "-....-"), ('_', "..--.-"), ('\"', ".-..-."), ('$', "...-..-"), ('@', ".--.-."),
(' ', "/")
};

public string Decode(string input)
{
var morseCodeToChar = _morseCode.ToDictionary(_ => _.MorseCode, _ => _.TextChar);
var decoded = input.Split(' ').Select(morseCodeToChar.GetValueOrDefault);
return string.Concat(decoded);
}

public string Encode(string morseInput)
{
var charToMorseCode = _morseCode.ToDictionary(_ => _.TextChar, _ => _.MorseCode);
var decoded = morseInput.Select(c => charToMorseCode.GetValueOrDefault(char.ToUpper(c)));
return string.Join(" ", decoded);
}
احمد
احمدOP2y ago
🤑🤑 !close
Accord
Accord2y ago
Closed!

Did you find this page helpful?