Skip to content

Instantly share code, notes, and snippets.

@eduan
Last active August 29, 2015 14:17
Show Gist options
  • Save eduan/ec1b302b5f40b55aa27a to your computer and use it in GitHub Desktop.
Save eduan/ec1b302b5f40b55aa27a to your computer and use it in GitHub Desktop.
var MORSE_CODE = {'\.-' : '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 decodeBits = function(bits){
var morse = [], bits = bits.split(''), last = '', trecho = '', frequency = {};
for (var i = bits.length - 1; i >= 0; i--) {
if(last.length > 0 && last != bits[i]){
var obj = frequency[last] ? frequency[last] : {};
obj[trecho.length] = trecho
frequency[last] = obj;
morse.push(trecho);
trecho = '';
}
trecho += bits[i].toString();
last = bits[i];
}
var obj = frequency[last] ? frequency[last] : {};
obj[trecho.length] = trecho
frequency[last] = obj;
morse.push(trecho);
morse.reverse()
var morseCod = {1: ['.', '-', '-'], 0 : ['', ' ', ' ']};
for (var i in frequency) {
var k = 0;
for(var j in frequency[i]){
frequency[i][j] = morseCod[i][k++];
}
}
var morseDecode = '';
for(var i in morse){
morseDecode += frequency[morse[i] > 0 ? 1: 0][morse[i].length];
}
return morseDecode;
}
var decodeMorse = function(morseCode){
var msg = morseCode.split(' ');
var msgDep = '';
for(var i in msg){
msgDep += (typeof MORSE_CODE[msg[i]] !== 'undefined') ? MORSE_CODE[msg[i]] : ' ';
}
return msgDep.trim().replace(/(\s+)/g,' ');
}
// decodeBits('1100110011001100000011000000111111001100111111001111110000000000000011001111110011111100111111000000110011001111110000001111110011001100000011')
// console.log(decodeBits('1'))
// console.log(decodeBits('101'))
// console.log(decodeBits('10001'))
// console.log(decodeBits('10111'))
// decodeBits('1110111')
// decodeBits('111')
// decodeBits('1111111')
// decodeBits('110011')
// decodeBits('111110000011111')
// decodeBits('11111100111111')
// decodeBits('01110')
// decodeBits('000000011100000')
// decodeBits('00011100010101010001000000011101110101110001010111000101000111010111010001110101110000000111010101000101110100011101110111000101110111000111010000000101011101000111011101110001110101011100000001011101110111000101011100011101110001011101110100010101000000011101110111000101010111000100010111010000000111000101010100010000000101110101000101110001110111010100011101011101110000000111010100011101110111000111011101000101110101110101110')
// decodeBits('11111111111111100000000000000011111000001111100000111110000011111000000000000000111110000000000000000000000000000000000011111111111111100000111111111111111000001111100000111111111111111000000000000000111110000011111000001111111111111110000000000000001111100000111110000000000000001111111111111110000011111000001111111111111110000011111000000000000000111111111111111000001111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000111110000000000000001111100000111111111111111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111111111111111000001111111111111110000000000000001111111111111110000011111000000000000000000000000000000000001111100000111110000011111111111111100000111110000000000000001111111111111110000011111111111111100000111111111111111000000000000000111111111111111000001111100000111110000011111111111111100000000000000000000000000000000000111110000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111111111111100000000000000011111111111111100000111111111111111000000000000000111110000011111111111111100000111111111111111000001111100000000000000011111000001111100000111110000000000000000000000000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111000001111111111111110000000000000001111100000000000000011111000001111111111111110000011111000000000000000000000000000000000001111111111111110000000000000001111100000111110000011111000001111100000000000000011111000000000000000000000000000000000001111100000111111111111111000001111100000111110000000000000001111100000111111111111111000000000000000111111111111111000001111111111111110000011111000001111100000000000000011111111111111100000111110000011111111111111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111111111111110000011111111111111100000111110000000000000001111100000111111111111111000001111100000111111111111111000001111100000111111111111111')
console.log('Org: ' + decodeMorse(decodeBits('1100110011001100000011000000111111001100111111001111110000000000000011001111110011111100111111000000110011001111110000001111110011001100000011')), 'Ex: HEY JUDE')
console.log('Org: ' + decodeMorse(decodeBits('1')), 'Ex: E')
console.log('Org: ' + decodeMorse(decodeBits('101')), 'IEx: ')
console.log('Org: ' + decodeMorse(decodeBits('10001')), 'Ex: EE')
console.log('Org: ' + decodeMorse(decodeBits('10111')), 'Ex: A')
console.log('Org: ' + decodeMorse(decodeBits('1110111')), 'Ex: M')
console.log('Org: ' + decodeMorse(decodeBits('111')), 'Ex: E')
console.log('Org: ' + decodeMorse(decodeBits('1111111')), 'Ex: E')
console.log('Org: ' + decodeMorse(decodeBits('110011')), 'Ex: I')
console.log('Org: ' + decodeMorse(decodeBits('111110000011111')), 'Ex: I')
console.log('Org: ' + decodeMorse(decodeBits('11111100111111')), 'Ex: M')
console.log('Org: ' + decodeMorse(decodeBits('01110')), 'Ex: E')
console.log('Org: ' + decodeMorse(decodeBits('000000011100000')), 'Ex: E')
console.log('Org: ' + decodeMorse(decodeBits('00011100010101010001000000011101110101110001010111000101000111010111010001110101110000000111010101000101110100011101110111000101110111000111010000000101011101000111011101110001110101011100000001011101110111000101011100011101110001011101110100010101000000011101110111000101010111000100010111010000000111000101010100010000000101110101000101110001110111010100011101011101110000000111010100011101110111000111011101000101110101110101110')), 'Ex: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.')
console.log('Org: ' + decodeMorse(decodeBits('11111111111111100000000000000011111000001111100000111110000011111000000000000000111110000000000000000000000000000000000011111111111111100000111111111111111000001111100000111111111111111000000000000000111110000011111000001111111111111110000000000000001111100000111110000000000000001111111111111110000011111000001111111111111110000011111000000000000000111111111111111000001111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000111110000000000000001111100000111111111111111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111111111111111000001111111111111110000000000000001111111111111110000011111000000000000000000000000000000000001111100000111110000011111111111111100000111110000000000000001111111111111110000011111111111111100000111111111111111000000000000000111111111111111000001111100000111110000011111111111111100000000000000000000000000000000000111110000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111111111111100000000000000011111111111111100000111111111111111000000000000000111110000011111111111111100000111111111111111000001111100000000000000011111000001111100000111110000000000000000000000000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111000001111111111111110000000000000001111100000000000000011111000001111111111111110000011111000000000000000000000000000000000001111111111111110000000000000001111100000111110000011111000001111100000000000000011111000000000000000000000000000000000001111100000111111111111111000001111100000111110000000000000001111100000111111111111111000000000000000111111111111111000001111111111111110000011111000001111100000000000000011111111111111100000111110000011111111111111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111111111111110000011111111111111100000111110000000000000001111100000111111111111111000001111100000111111111111111000001111100000111111111111111')), 'Ex: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.')
var decodeBits = function(bits){
var morse = [], bits = bits.split(''), last = '', trecho = '', frequency = {};
for (var i = bits.length - 1; i >= 0; i--) {
if(last.length > 0 && last != bits[i]){
morse.push(trecho);
trecho = '';
}
trecho += bits[i].toString();
last = bits[i];
}
morse.push(trecho);
console.log(morse.reverse());
console.log(frequency.sort());
}
console.log(decodeBits('00011100010101010001000000011101110101110001010111000101000111010111010001110101110000000111010101000101110100011101110111000101110111000111010000000101011101000111011101110001110101011100000001011101110111000101011100011101110001011101110100010101000000011101110111000101010111000100010111010000000111000101010100010000000101110101000101110001110111010100011101011101110000000111010100011101110111000111011101000101110101110101110'));
console.log(decodeBits('1'));
console.log(decodeBits('101'));
console.log(decodeBits('10001'));
console.log(decodeBits('10111'));
console.log(decodeBits('01110'));
console.log(decodeBits('11111100111111'));
console.log(decodeBits('11111100111111'));
console.log(decodeBits('111110000011111'));
console.log(decodeBits('000000011100000'));
console.log(decodeBits('00011100010101010001000000011101110101110001010111000101000111010111010001110101110000000111010101000101110100011101110111000101110111000111010000000101011101000111011101110001110101011100000001011101110111000101011100011101110001011101110100010101000000011101110111000101010111000100010111010000000111000101010100010000000101110101000101110001110111010100011101011101110000000111010100011101110111000111011101000101110101110101110'));
console.log(decodeBits('11111111111111100000000000000011111000001111100000111110000011111000000000000000111110000000000000000000000000000000000011111111111111100000111111111111111000001111100000111111111111111000000000000000111110000011111000001111111111111110000000000000001111100000111110000000000000001111111111111110000011111000001111111111111110000011111000000000000000111111111111111000001111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000111110000000000000001111100000111111111111111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111111111111111000001111111111111110000000000000001111111111111110000011111000000000000000000000000000000000001111100000111110000011111111111111100000111110000000000000001111111111111110000011111111111111100000111111111111111000000000000000111111111111111000001111100000111110000011111111111111100000000000000000000000000000000000111110000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111111111111100000000000000011111111111111100000111111111111111000000000000000111110000011111111111111100000111111111111111000001111100000000000000011111000001111100000111110000000000000000000000000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111000001111111111111110000000000000001111100000000000000011111000001111111111111110000011111000000000000000000000000000000000001111111111111110000000000000001111100000111110000011111000001111100000000000000011111000000000000000000000000000000000001111100000111111111111111000001111100000111110000000000000001111100000111111111111111000000000000000111111111111111000001111111111111110000011111000001111100000000000000011111111111111100000111110000011111111111111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111111111111110000011111111111111100000111110000000000000001111100000111111111111111000001111100000111111111111111000001111100000111111111111111'));
199.27.76.133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment