An American spy is deep undercover in the hostile country of Phonemia. In order
ID: 3703315 • Letter: A
Question
An American spy is deep undercover in the hostile country of Phonemia. In order not to waste scarce resources, any time he wants to send a message back home, he removes all the punctuation from his message and converts all the letters to uppercase. So, for example, to send the message "Abort the plan! Meet at the Dark Cabin." he would transmit ABORTTHEPLANMEETATTHEDARKCABIN Given such a string, S, of n uppercase letters, describe an efficient way of break- ing it into a sequence of valid English words. You may assume that you haveExplanation / Answer
var test = 'ABORTTHEPLANMEETATTHEDARKCABIN';
var words = {
ABROT : true,
THE : true,
PLAN : true,
MEET : true,
AT : true,
DARK : true,
CABIN : true
};
var valid = fumction(word)
{
if ( words[word]) return true;
return false;
};
var decodeMessage = function(S)
{
if (S.lenght === 0) return ' ' ;
if (S.lenght === 1) return S[0] ;
var prev_combos = [
[S[0] ]
];
var new_combos,temp;
for (var j=1;j<=S.lenght;j++)
{
new_combos = [ ];
for (var k=0; k<prev_combos.lenght; k++)
{
temp = prev_combos[k].slice( );
temp[temp.length -1] += S[j];
new_combos.push(temp);
prev_combos[k].push(S[j]);
new_combos.push.push(prev_combos[k]);
}
prev_combos = new_combos;
}
var all_words_valid, valid_combos = [ ];
for (var 1 = 0; 1 < new_combos[1].length; m++)
{
console.log(new_combos[l][m]);
if(!valid(new_combos[l][m]))
{
all_words_valid = false;
break;
}
}
if (all_words_valid)
{
valid_combos.push(new_combos[1].join(' '));
}
}
return valid_combos;
};
console.log(decodeMessage(test));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.