Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 have

Explanation / 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));

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote