Skip to content

Instantly share code, notes, and snippets.

@m00shm00sh
Created January 15, 2015 08:02
Show Gist options
  • Save m00shm00sh/6a3ac47e5796332d74d9 to your computer and use it in GitHub Desktop.
Save m00shm00sh/6a3ac47e5796332d74d9 to your computer and use it in GitHub Desktop.
Morse code encoder, with extra ( ͡° ͜ʖ ͡°)
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static readonly Dictionary<char, string> morseCodes
= new Dictionary<char, string> {
// ITU-R M.1677-1 1.1.1
{'a', ".-"}, {'i', ".."}, {'r', ".-."},
{'b', "-..."}, {'j', ".---"}, {'s', "..."},
{'c', "-.-."}, {'k', "-.-"}, {'t', "-"},
{'d', "-.."}, {'l', ".-.."}, {'u', "..-"},
{'e', "."}, {'m', "--"}, {'v', "...-"},
{'é', "..-.."}, {'n', "-."}, {'w', ".--"},
{'f', "..-."}, {'o', "---"}, {'x', "-..-"},
{'g', "--."}, {'p', ".--."}, {'y', "-.--"},
{'h', "...."}, {'q', "--.-"}, {'z', "--.."},
// ITU-R M.1677-1 1.1.2
{'1', ".----"}, {'6', "-...."},
{'2', "..---"}, {'7', "--..."},
{'3', "...--"}, {'8', "---.."},
{'4', "....-"}, {'9', "----."},
{'5', "....."}, {'0', "-----"},
// ITU-R M.1677-1 1.1.3
{'.' , ".-.-.-"},
{',' , "--..--"},
{':' , "---..."},
{'?' , "..--.."},
{'\'', ".----."},
{'-' , "-....-"},
{'/' , "-..-." },
{'(' , "-.--." },
{')' , "-.--.-"},
{'\"', ".-..-."},
{'=' , "-...-" },
// "Understood" unimplemented
// "Error" unimplemented
{'+' , ".-.-." },
// "Invitation to transmit" unimplemented
// "Wait" unimplemented
// "End of work" unimplemented
// "Starting signal" unimplemented
{'*' , "-..-" },
{'@' , ".--.-."}
};
static string EncodeMorse(string text) {
bool isWhitespace = false;
StringBuilder letterStream = new StringBuilder();
foreach (char symbol in text.Trim()) {
if (Char.IsWhiteSpace(symbol) && !isWhitespace) {
isWhitespace = true;
letterStream.Append("...");
}
if (!(isWhitespace = Char.IsWhiteSpace(symbol))) {
string morse;
if (morseCodes.TryGetValue(Char.ToLower(symbol), out morse)) {
letterStream.Append(morse);
letterStream.Append(".");
}
}
}
StringBuilder ret = new StringBuilder();
bool firstChar = true;
foreach (char symbol in letterStream.ToString()) {
if (!firstChar)
ret.Append(".");
ret.Append(symbol);
firstChar = false;
}
return ret.ToString();
}
static readonly Dictionary<char,string> lolDongs
= new Dictionary<char, string> {
{ '-', "( ͡° ͜ʖ ͡°)"}
};
static string Dongs(string text) {
StringBuilder stream = new StringBuilder();
foreach (char symbol in text) {
string dong;
if (lolDongs.TryGetValue(symbol, out dong))
stream.Append(dong);
else
stream.Append(symbol);
}
return stream.ToString();
}
static void Main() {
string line;
while ((line = Console.ReadLine()) != null) {
Console.WriteLine(Dongs(EncodeMorse(line)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment