From e86bfe451468464825f42320f8c70df27dc8f2ff Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Fri, 18 Aug 2017 12:22:03 -0500 Subject: [PATCH 1/9] start of pirate js version --- CodingChallenge.PirateSpeak/Solution.cs | 48 ++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 2c77075..848e5a7 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Collections.ObjectModel namespace CodingChallenge.PirateSpeak { @@ -7,7 +8,52 @@ public class Solution { public string[] GetPossibleWords(string jumble, string[] dictionary) { - throw new NotImplementedException(); + // throw new NotImplementedException(); + } + } +} + + +// split letters into indexes of array +// put all in lowercase +// will need to get array length +// foreach +// search dictionary for all letters starting with first +// then followed with all for second, all for third, etc +// if match, run with next indexes + +// or + +// array of all scrambled combinations +// then match indexes to dictionary +// add matches to new array + + + + +function setUp (jumble, dictionary){ + let letters = jumble.ToLower().split(''); //set up array of letters from user entry + let n = letters.length; // determine length of array for future loops + + function Unscramble (letters, n){ + let stop = false; + let mixletters = []; // temp storage for each scramble, to be cleared each time + let firstmix = []; // firstRun results + let finalmixes = []; // all final possible scrambles + while (n!=0){ + function firstRun (letters, n){ + for (let i = 0; i < n; i++) + { + mixletters.push(i); // create scramble + } + let mixword = mixletters.join(','); //join letters together into one scramble + firstmix.push(mixword); // add scramble to results array + let mixletters = []; // clear temp storage + }; + let n = n-1; //reduce counter + let popped = letters.pop(); //remove last index + let reworked = letters.splice(1,0,popped); //add last index to second spot + firstRun(reworked, n); } } } \ No newline at end of file From 2dc2791fb6825103e68d1d4b0f4a3193eddc3a62 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Fri, 18 Aug 2017 13:41:01 -0500 Subject: [PATCH 2/9] finished second scramble --- CodingChallenge.PirateSpeak/Solution.cs | 33 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 848e5a7..8bcc5fd 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -11,7 +11,7 @@ public string[] GetPossibleWords(string jumble, string[] dictionary) // throw new NotImplementedException(); } } -} +}; // split letters into indexes of array @@ -35,7 +35,7 @@ function setUp (jumble, dictionary){ let letters = jumble.ToLower().split(''); //set up array of letters from user entry let n = letters.length; // determine length of array for future loops - function Unscramble (letters, n){ + function firstScramble (letters, n){ let stop = false; let mixletters = []; // temp storage for each scramble, to be cleared each time let firstmix = []; // firstRun results @@ -48,12 +48,39 @@ function firstRun (letters, n){ } let mixword = mixletters.join(','); //join letters together into one scramble firstmix.push(mixword); // add scramble to results array + finalmixes.push(mixword); //add scramble to final results let mixletters = []; // clear temp storage }; let n = n-1; //reduce counter let popped = letters.pop(); //remove last index - let reworked = letters.splice(1,0,popped); //add last index to second spot + let reworked = letters.splice(0,0,popped); //add last index to first spot firstRun(reworked, n); } + secondUnscramble(firstmix); } + + function secondScramble (words){ // function to take firstmix and create more scrambled options + let n = letters.length; + foreach (var word in words) + { + let splitWord = word.split(''); + let spliced = splitWord.splice(1,1); //swap around first two indexes + let reworked = splitWord.splice(0,0,spliced); + for (let i = 0; i < reworked.length; i++) //run through same process as function firstRun + { + mixletters.push(i); // create scramble + } + let mixword = mixletters.join(','); //join letters together into one scramble + firstmix.push(mixword); // add scramble to results array + finalmixes.push(mixword); //add scramble to final results + let mixletters = []; // clear temp storage + + } + compareWords(finalmixes); + } + + function compareWords(scrambled, dictionary){ + + } + } \ No newline at end of file From 14d11a127e72194009a11786f483aff4bdba786a Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Fri, 18 Aug 2017 13:53:26 -0500 Subject: [PATCH 3/9] adjustments to while loops --- CodingChallenge.PirateSpeak/Solution.cs | 44 ++++++++++++++----------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 8bcc5fd..51ff429 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -33,16 +33,15 @@ public string[] GetPossibleWords(string jumble, string[] dictionary) function setUp (jumble, dictionary){ let letters = jumble.ToLower().split(''); //set up array of letters from user entry - let n = letters.length; // determine length of array for future loops function firstScramble (letters, n){ - let stop = false; + let n = letters.length; // determine length of array let mixletters = []; // temp storage for each scramble, to be cleared each time let firstmix = []; // firstRun results let finalmixes = []; // all final possible scrambles while (n!=0){ function firstRun (letters, n){ - for (let i = 0; i < n; i++) + for (let i = 0; i < letters.length; i++) { mixletters.push(i); // create scramble } @@ -50,32 +49,39 @@ function firstRun (letters, n){ firstmix.push(mixword); // add scramble to results array finalmixes.push(mixword); //add scramble to final results let mixletters = []; // clear temp storage + let n = n-1; //reduce counter + let popped = letters.pop(); //remove last index + let reworked = letters.splice(0,0,popped); //add last index to first spot + firstRun(reworked, n); }; - let n = n-1; //reduce counter - let popped = letters.pop(); //remove last index - let reworked = letters.splice(0,0,popped); //add last index to first spot - firstRun(reworked, n); - } + }; secondUnscramble(firstmix); } function secondScramble (words){ // function to take firstmix and create more scrambled options - let n = letters.length; foreach (var word in words) { let splitWord = word.split(''); let spliced = splitWord.splice(1,1); //swap around first two indexes let reworked = splitWord.splice(0,0,spliced); - for (let i = 0; i < reworked.length; i++) //run through same process as function firstRun - { - mixletters.push(i); // create scramble - } - let mixword = mixletters.join(','); //join letters together into one scramble - firstmix.push(mixword); // add scramble to results array - finalmixes.push(mixword); //add scramble to final results - let mixletters = []; // clear temp storage - - } + let n = splitWord.length; + while (n!=0){ // run reworked word through same firstScramble process + function firstRun (letters, n){ + for (let i = 0; i < letters.length; i++) + { + mixletters.push(i); // create scramble + } + let mixword = mixletters.join(','); //join letters together into one scramble + firstmix.push(mixword); // add scramble to results array + finalmixes.push(mixword); //add scramble to final results + let mixletters = []; // clear temp storage + let n = n-1; //reduce counter + let popped = letters.pop(); //remove last index + let reworked = letters.splice(0,0,popped); //add last index to first spot + firstRun(reworked, n); + }; + }; + }; compareWords(finalmixes); } From 153c0a62cbd40bcc4e1a24e9d6215cd2d09ee5c1 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Mon, 21 Aug 2017 14:45:00 -0500 Subject: [PATCH 4/9] adding additional unscrambler --- CodingChallenge.PirateSpeak/Solution.cs | 57 ++++++++++++++++--------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 51ff429..e26cec9 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -34,55 +34,70 @@ public string[] GetPossibleWords(string jumble, string[] dictionary) function setUp (jumble, dictionary){ let letters = jumble.ToLower().split(''); //set up array of letters from user entry - function firstScramble (letters, n){ - let n = letters.length; // determine length of array + function firstUnscramble (letters){ let mixletters = []; // temp storage for each scramble, to be cleared each time - let firstmix = []; // firstRun results - let finalmixes = []; // all final possible scrambles - while (n!=0){ - function firstRun (letters, n){ + let firstMix = []; // firstRun results + let finalMix = []; // all final possible scrambles + function firstRun (letters, n){ + let n = letters.length; // determine length of array + if (n!=0){ for (let i = 0; i < letters.length; i++) { mixletters.push(i); // create scramble } - let mixword = mixletters.join(','); //join letters together into one scramble - firstmix.push(mixword); // add scramble to results array - finalmixes.push(mixword); //add scramble to final results - let mixletters = []; // clear temp storage - let n = n-1; //reduce counter + let mixWord = mixletters.join(','); //join letters together into one scramble + firstMix.push(mixWord); // add scramble to results array + finalMix.push(mixWord); //add scramble to final results + mixletters = []; // clear temp storage + n = n-1; //reduce counter let popped = letters.pop(); //remove last index let reworked = letters.splice(0,0,popped); //add last index to first spot firstRun(reworked, n); }; + else { + secondRun(letters) + } + + function secondRun (letters){ + let n = letters.length; + if (n!=0){ + for (int i = 0; i < length; i++) + { + let spliced = letters.splice((n-i),1); + let reworked = + + } + } }; - secondUnscramble(firstmix); + }; + secondUnscramble(firstMix); } - function secondScramble (words){ // function to take firstmix and create more scrambled options + function secondUnscramble (words){ // function to take firstMix and create more scrambled options foreach (var word in words) { let splitWord = word.split(''); let spliced = splitWord.splice(1,1); //swap around first two indexes let reworked = splitWord.splice(0,0,spliced); let n = splitWord.length; - while (n!=0){ // run reworked word through same firstScramble process - function firstRun (letters, n){ + function firstRun (letters, n){ + while (n!=0){ // run reworked word through same firstUnscramble process for (let i = 0; i < letters.length; i++) { mixletters.push(i); // create scramble } - let mixword = mixletters.join(','); //join letters together into one scramble - firstmix.push(mixword); // add scramble to results array - finalmixes.push(mixword); //add scramble to final results - let mixletters = []; // clear temp storage - let n = n-1; //reduce counter + let mixWord = mixletters.join(','); //join letters together into one scramble + firstMix.push(mixWord); // add scramble to results array + finalMix.push(mixWord); //add scramble to final results + mixletters = []; // clear temp storage + n = n-1; //reduce counter let popped = letters.pop(); //remove last index let reworked = letters.splice(0,0,popped); //add last index to first spot firstRun(reworked, n); }; }; }; - compareWords(finalmixes); + compareWords(finalMix, dictionary); } function compareWords(scrambled, dictionary){ From 59c9bd9fe89db9c701b031b131a9cd2cb82aecf5 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Mon, 21 Aug 2017 16:13:29 -0500 Subject: [PATCH 5/9] still calculating counter work --- CodingChallenge.PirateSpeak/Solution.cs | 46 ++++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index e26cec9..16de5d0 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -38,34 +38,38 @@ function firstUnscramble (letters){ let mixletters = []; // temp storage for each scramble, to be cleared each time let firstMix = []; // firstRun results let finalMix = []; // all final possible scrambles - function firstRun (letters, n){ + let k = Math.pow(letters.length, 2) + 1; + function mainRun (letters){ let n = letters.length; // determine length of array if (n!=0){ - for (let i = 0; i < letters.length; i++) - { - mixletters.push(i); // create scramble + function firstRun (letters){ // rotate order of letters and push to array + for (let i = 0; i < letters.length; i++) + { + mixletters.push(i); // create scramble + } + let mixWord = mixletters.join(','); //join letters together into one scramble + firstMix.push(mixWord); // add scramble to results array + finalMix.push(mixWord); //add scramble to final results + mixletters = []; // clear temp storage + n = n-1; //reduce counter + k= k-1 + let popped = letters.pop(); //remove last index + let reworked = letters.splice(0,0,popped); //add last index to first spot + firstRun(reworked); } - let mixWord = mixletters.join(','); //join letters together into one scramble - firstMix.push(mixWord); // add scramble to results array - finalMix.push(mixWord); //add scramble to final results - mixletters = []; // clear temp storage - n = n-1; //reduce counter - let popped = letters.pop(); //remove last index - let reworked = letters.splice(0,0,popped); //add last index to first spot - firstRun(reworked, n); }; else { - secondRun(letters) + break; } - function secondRun (letters){ - let n = letters.length; + function secondRun (newLetters){ + let n = newLetters.length; if (n!=0){ - for (int i = 0; i < length; i++) + for (int i = 0; i < n; i++) { - let spliced = letters.splice((n-i),1); - let reworked = - + let spliced = newLetters.splice((n-(i+1)),1); + let reworked = newLetters.splice(0,0,spliced); + mainRun(reworked); } } }; @@ -80,7 +84,7 @@ function secondUnscramble (words){ // function to take firstMix and create mor let spliced = splitWord.splice(1,1); //swap around first two indexes let reworked = splitWord.splice(0,0,spliced); let n = splitWord.length; - function firstRun (letters, n){ + function mainRun (letters, n){ while (n!=0){ // run reworked word through same firstUnscramble process for (let i = 0; i < letters.length; i++) { @@ -93,7 +97,7 @@ function firstRun (letters, n){ n = n-1; //reduce counter let popped = letters.pop(); //remove last index let reworked = letters.splice(0,0,popped); //add last index to first spot - firstRun(reworked, n); + mainRun(reworked, n); }; }; }; From c432227b9c3e3275ed0f5fe04b582714bca329f9 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Tue, 22 Aug 2017 20:53:59 -0500 Subject: [PATCH 6/9] reworked code, still looping incorrectly --- CodingChallenge.PirateSpeak/Solution.cs | 152 +++++++++++++----------- 1 file changed, 85 insertions(+), 67 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 16de5d0..376edf8 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -34,78 +34,96 @@ public string[] GetPossibleWords(string jumble, string[] dictionary) function setUp (jumble, dictionary){ let letters = jumble.ToLower().split(''); //set up array of letters from user entry - function firstUnscramble (letters){ - let mixletters = []; // temp storage for each scramble, to be cleared each time - let firstMix = []; // firstRun results - let finalMix = []; // all final possible scrambles - let k = Math.pow(letters.length, 2) + 1; - function mainRun (letters){ - let n = letters.length; // determine length of array - if (n!=0){ - function firstRun (letters){ // rotate order of letters and push to array - for (let i = 0; i < letters.length; i++) - { - mixletters.push(i); // create scramble - } - let mixWord = mixletters.join(','); //join letters together into one scramble - firstMix.push(mixWord); // add scramble to results array - finalMix.push(mixWord); //add scramble to final results - mixletters = []; // clear temp storage - n = n-1; //reduce counter - k= k-1 - let popped = letters.pop(); //remove last index - let reworked = letters.splice(0,0,popped); //add last index to first spot - firstRun(reworked); - } - }; - else { - break; - } - function secondRun (newLetters){ - let n = newLetters.length; - if (n!=0){ - for (int i = 0; i < n; i++) - { - let spliced = newLetters.splice((n-(i+1)),1); - let reworked = newLetters.splice(0,0,spliced); - mainRun(reworked); - } - } - }; - }; - secondUnscramble(firstMix); + + function compareWords(scrambled, dictionary){ + } - function secondUnscramble (words){ // function to take firstMix and create more scrambled options - foreach (var word in words) - { - let splitWord = word.split(''); - let spliced = splitWord.splice(1,1); //swap around first two indexes - let reworked = splitWord.splice(0,0,spliced); - let n = splitWord.length; - function mainRun (letters, n){ - while (n!=0){ // run reworked word through same firstUnscramble process - for (let i = 0; i < letters.length; i++) - { - mixletters.push(i); // create scramble - } - let mixWord = mixletters.join(','); //join letters together into one scramble - firstMix.push(mixWord); // add scramble to results array - finalMix.push(mixWord); //add scramble to final results - mixletters = []; // clear temp storage - n = n-1; //reduce counter - let popped = letters.pop(); //remove last index - let reworked = letters.splice(0,0,popped); //add last index to first spot - mainRun(reworked, n); - }; - }; - }; - compareWords(finalMix, dictionary); +} + + +function firstUnscramble(letters) { + let mixletters = []; // temp storage for each scramble, to be cleared each time + let firstMix = []; // firstRun results + let finalMix = []; // all final possible scrambles + let k = Math.pow(letters.length, 2) + 1; + console.log("k", k); + let n = letters.length; // determine length of array + console.log("n", n); + + + var firstRun = function(word){ + console.log("okay", word); + let letters = word.split(''); + let l = letters.length; + console.log("word", letters); + console.log("l", l); + if (k !== 0) { + // rotate order of letters and push to array + for (let i = 0; i < letters.length; i++) { + mixletters.push(letters[i]); + } // create scramble + + let mixWord = mixletters.join(""); //join letters together into one scramble + console.log("mixletters", mixletters); + // firstMix.push(mixWord); // add scramble to results array + console.log("first", firstMix); + finalMix.push(mixWord); //add scramble to final results + console.log("final", finalMix); + k = k - 1; + mixletters = []; // clear temp storage + l = l - 1; //reduce counter + console.log("l2", l); + console.log("letters", letters); + let popped2 = letters.pop(); //remove last index + console.log("pop", popped2); + let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot + mixWord = letters.join(""); + console.log("reworked", mixWord); + firstRun(mixWord); + + } else { + console.log("done"); + console.log("finalMix", finalMix); + console.log("firstMix", firstMix); } + }; - function compareWords(scrambled, dictionary){ + for (let i = 0; i <= letters.length; i++) { + if (n !== 0) { + let spliced = letters.splice(i, 1); + console.log("spliced", spliced); + let popped = spliced.pop(); + let reworked = letters.splice(0, 0, popped); + console.log("rework", letters); + // let redo = letters; + // console.log("redo", redo) + // firstRun(redo); + let mixWord = letters.join(""); + firstMix.push(mixWord); + n = n - 1; + let respliced = letters.splice(0, 1); + let popped2 = respliced.pop(); + let fixed = letters.splice(i, 0, popped2); + + console.log("n", n); + console.log("firstMix", firstMix); + } else { + console.log("first wave done"); + // firstRun() + const run1 = firstMix.length; + console.log("run1", run1); + for(let i = 0; i < run1; i++){ + console.log("mixfirst", firstMix[i]) + firstRun(firstMix[i]); + } + } } + } + + + -} \ No newline at end of file +firstUnscramble(["1", "2", "3", "4"]); \ No newline at end of file From b73333e035e8af8ec647c54e65251e7c032b4cb2 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Tue, 22 Aug 2017 21:59:58 -0500 Subject: [PATCH 7/9] corrected permutation count, string divider --- CodingChallenge.PirateSpeak/Solution.cs | 85 ++++++++++++------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index 376edf8..b3cade9 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -43,46 +43,50 @@ function compareWords(scrambled, dictionary){ } -function firstUnscramble(letters) { +function firstUnscramble(jumble) { + let letters = jumble.toLowerCase().split(""); + console.log("firstjumble", letters); let mixletters = []; // temp storage for each scramble, to be cleared each time let firstMix = []; // firstRun results let finalMix = []; // all final possible scrambles - let k = Math.pow(letters.length, 2) + 1; + let k = 1; + for (let i = 1; i < letters.length + 1; i++){ + k = k * i; + console.log("k", k); + } console.log("k", k); let n = letters.length; // determine length of array console.log("n", n); - - var firstRun = function(word){ - console.log("okay", word); - let letters = word.split(''); - let l = letters.length; - console.log("word", letters); - console.log("l", l); - if (k !== 0) { - // rotate order of letters and push to array - for (let i = 0; i < letters.length; i++) { - mixletters.push(letters[i]); - } // create scramble - - let mixWord = mixletters.join(""); //join letters together into one scramble - console.log("mixletters", mixletters); - // firstMix.push(mixWord); // add scramble to results array - console.log("first", firstMix); - finalMix.push(mixWord); //add scramble to final results - console.log("final", finalMix); - k = k - 1; - mixletters = []; // clear temp storage - l = l - 1; //reduce counter - console.log("l2", l); - console.log("letters", letters); - let popped2 = letters.pop(); //remove last index - console.log("pop", popped2); - let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot - mixWord = letters.join(""); - console.log("reworked", mixWord); - firstRun(mixWord); - + var firstRun = function(word) { + // console.log("okay", word); + let letters = word.split(""); + let l = letters.length; + console.log("word", letters); + console.log("k", k); + if (k !== 0) { + // rotate order of letters and push to array + for (let i = 0; i < letters.length; i++) { + mixletters.push(letters[i]); + } // create scramble + + let mixWord = mixletters.join(""); //join letters together into one scramble + console.log("mixletters", mixletters); + // firstMix.push(mixWord); // add scramble to results array + console.log("first", firstMix); + finalMix.push(mixWord); //add scramble to final results + console.log("final", finalMix); + k = k - 1; + mixletters = []; // clear temp storage + l = l - 1; //reduce counter + console.log("l2", l); + console.log("letters", letters); + let popped2 = letters.pop(); //remove last index + console.log("pop", popped2); + let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot + mixWord = letters.join(""); + console.log("reworked", mixWord); + firstRun(mixWord); } else { console.log("done"); console.log("finalMix", finalMix); @@ -111,19 +115,14 @@ function firstUnscramble(letters) { console.log("firstMix", firstMix); } else { console.log("first wave done"); - // firstRun() const run1 = firstMix.length; console.log("run1", run1); - for(let i = 0; i < run1; i++){ - console.log("mixfirst", firstMix[i]) - firstRun(firstMix[i]); + for (let i = 0; i < run1; i++) { + console.log("mixfirst", firstMix[i]); + // firstRun(firstMix[i]); } } - - } } +} - - - -firstUnscramble(["1", "2", "3", "4"]); \ No newline at end of file +firstUnscramble("1234"); From ec5a3f051e24fc289af3b77bc5dbfb98f81ed411 Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Tue, 22 Aug 2017 23:52:46 -0500 Subject: [PATCH 8/9] working on locating missing scrambles --- CodingChallenge.PirateSpeak/Solution.cs | 115 +++++++++++++++++++++++- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index b3cade9..b248c17 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -55,7 +55,7 @@ function firstUnscramble(jumble) { console.log("k", k); } console.log("k", k); - let n = letters.length; // determine length of array + let n = letters.length; // save length of array console.log("n", n); var firstRun = function(word) { @@ -63,7 +63,7 @@ function firstUnscramble(jumble) { let letters = word.split(""); let l = letters.length; console.log("word", letters); - console.log("k", k); + console.log("l1", l); if (k !== 0) { // rotate order of letters and push to array for (let i = 0; i < letters.length; i++) { @@ -80,13 +80,14 @@ function firstUnscramble(jumble) { mixletters = []; // clear temp storage l = l - 1; //reduce counter console.log("l2", l); + console.log("k2", k); console.log("letters", letters); let popped2 = letters.pop(); //remove last index console.log("pop", popped2); let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot mixWord = letters.join(""); console.log("reworked", mixWord); - firstRun(mixWord); + // firstRun(mixWord); } else { console.log("done"); console.log("finalMix", finalMix); @@ -119,7 +120,113 @@ function firstUnscramble(jumble) { console.log("run1", run1); for (let i = 0; i < run1; i++) { console.log("mixfirst", firstMix[i]); - // firstRun(firstMix[i]); + firstRun(firstMix[i]); + } + } + } +} + +firstUnscramble("123"); + + + + + + +function firstUnscramble(jumble) { + let letters = jumble.toLowerCase().split(""); + console.log("firstjumble", letters); + let mixletters = []; // temp storage for each scramble, to be cleared each time + let firstMix = []; // firstRun results + let revMix = []; + let finalMix = []; // all final possible scrambles + let k = 2; + for (let i = 1; i < letters.length + 1; i++) { + k = k * i; + console.log("k", k); + } + console.log("k", k); + let n = letters.length; // save length of array + console.log("n", n); + + var firstRun = function(word) { + // console.log("okay", word); + let letters = word.split(""); + let l = letters.length; + console.log("word", letters); + console.log("l1", l); + if (k !== 0) { + // rotate order of letters and push to array + for (let i = 0; i < letters.length; i++) { + mixletters.push(letters[i]); + revMix.unshift(letters[i]); + } // create scramble + + let mixWord = mixletters.join(""); //join letters together into one scramble + let revWord = revMix.join(""); + console.log("mixletters", mixletters); + // firstMix.push(mixWord); // add scramble to results array + console.log("first", firstMix); + // for (var j = 0; j < firstMix.length; j++) { + // if (mixWord != firstMix[j]) { + // firstMix.push(mixWord); //add scramble to final results + // } else { + // break; + // } + // } + console.log("final", finalMix); + firstMix.push(mixWord); + firstMix.push(revWord); + k = k - 2; + mixletters = []; // clear temp storage + revMix = []; + l = l - 1; //reduce counter + console.log("l2", l); + console.log("k2", k); + console.log("letters", letters); + let popped2 = letters.pop(); //remove last index + console.log("pop", popped2); + let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot + mixWord = letters.join(""); + console.log("reworked", mixWord); + // firstMix.push(mixWord); + // firstRun(mixWord); + } else { + console.log("done"); + console.log("finalMix", finalMix); + console.log("firstMix", firstMix); + console.log("firstlength", firstMix.length); + console.log("finallength", finalMix.length); + } + }; + + for (let i = 0; i <= letters.length; i++) { + if (n !== 0) { + let spliced = letters.splice(i, 1); + console.log("spliced", spliced); + let popped = spliced.pop(); + let reworked = letters.splice(0, 0, popped); + console.log("rework", letters); + // let redo = letters; + // console.log("redo", redo) + // firstRun(redo); + let mixWord = letters.join(""); + firstMix.push(mixWord); + n = n - 1; + let respliced = letters.splice(0, 1); + let popped2 = respliced.pop(); + let fixed = letters.splice(i, 0, popped2); + + console.log("n", n); + console.log("firstMix", firstMix); + } else { + console.log("first wave done"); + // const run1 = firstMix.length; + // console.log("run1", run1); + console.log("Kloop", k); + for (let i = 0; i < firstMix.length; i++) { + console.log("mixfirst", firstMix[i]); + firstRun(firstMix[i]); } } } From 420cef70c0688212ac9eb8e56588a271ff4d062a Mon Sep 17 00:00:00 2001 From: Matt Latham Date: Wed, 23 Aug 2017 00:20:56 -0500 Subject: [PATCH 9/9] removed console logs and added comments --- CodingChallenge.PirateSpeak/Solution.cs | 165 +++--------------------- 1 file changed, 21 insertions(+), 144 deletions(-) diff --git a/CodingChallenge.PirateSpeak/Solution.cs b/CodingChallenge.PirateSpeak/Solution.cs index b248c17..5b48bdc 100644 --- a/CodingChallenge.PirateSpeak/Solution.cs +++ b/CodingChallenge.PirateSpeak/Solution.cs @@ -31,130 +31,29 @@ public string[] GetPossibleWords(string jumble, string[] dictionary) -function setUp (jumble, dictionary){ - let letters = jumble.ToLower().split(''); //set up array of letters from user entry - function compareWords(scrambled, dictionary){ - - } - -} - - -function firstUnscramble(jumble) { - let letters = jumble.toLowerCase().split(""); - console.log("firstjumble", letters); - let mixletters = []; // temp storage for each scramble, to be cleared each time - let firstMix = []; // firstRun results - let finalMix = []; // all final possible scrambles - let k = 1; - for (let i = 1; i < letters.length + 1; i++){ - k = k * i; - console.log("k", k); - } - console.log("k", k); - let n = letters.length; // save length of array - console.log("n", n); - - var firstRun = function(word) { - // console.log("okay", word); - let letters = word.split(""); - let l = letters.length; - console.log("word", letters); - console.log("l1", l); - if (k !== 0) { - // rotate order of letters and push to array - for (let i = 0; i < letters.length; i++) { - mixletters.push(letters[i]); - } // create scramble - - let mixWord = mixletters.join(""); //join letters together into one scramble - console.log("mixletters", mixletters); - // firstMix.push(mixWord); // add scramble to results array - console.log("first", firstMix); - finalMix.push(mixWord); //add scramble to final results - console.log("final", finalMix); - k = k - 1; - mixletters = []; // clear temp storage - l = l - 1; //reduce counter - console.log("l2", l); - console.log("k2", k); - console.log("letters", letters); - let popped2 = letters.pop(); //remove last index - console.log("pop", popped2); - let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot - mixWord = letters.join(""); - console.log("reworked", mixWord); - // firstRun(mixWord); - } else { - console.log("done"); - console.log("finalMix", finalMix); - console.log("firstMix", firstMix); - } - }; - - for (let i = 0; i <= letters.length; i++) { - if (n !== 0) { - let spliced = letters.splice(i, 1); - console.log("spliced", spliced); - let popped = spliced.pop(); - let reworked = letters.splice(0, 0, popped); - console.log("rework", letters); - // let redo = letters; - // console.log("redo", redo) - // firstRun(redo); - let mixWord = letters.join(""); - firstMix.push(mixWord); - n = n - 1; - let respliced = letters.splice(0, 1); - let popped2 = respliced.pop(); - let fixed = letters.splice(i, 0, popped2); - - console.log("n", n); - console.log("firstMix", firstMix); - } else { - console.log("first wave done"); - const run1 = firstMix.length; - console.log("run1", run1); - for (let i = 0; i < run1; i++) { - console.log("mixfirst", firstMix[i]); - firstRun(firstMix[i]); - } - } - } -} - -firstUnscramble("123"); - function firstUnscramble(jumble) { - let letters = jumble.toLowerCase().split(""); - console.log("firstjumble", letters); + let letters = jumble.toLowerCase().split(""); // set up array of letters from user entry let mixletters = []; // temp storage for each scramble, to be cleared each time let firstMix = []; // firstRun results - let revMix = []; + let revMix = []; // reverse results let finalMix = []; // all final possible scrambles - let k = 2; - for (let i = 1; i < letters.length + 1; i++) { + let k = 2; // set t0 double permutations + for (let i = 1; i < letters.length + 1; i++) { // setting up permutations counter k = k * i; - console.log("k", k); } - console.log("k", k); let n = letters.length; // save length of array - console.log("n", n); var firstRun = function(word) { - // console.log("okay", word); - let letters = word.split(""); + let letters = word.split(""); //turn jumble into letters let l = letters.length; - console.log("word", letters); - console.log("l1", l); if (k !== 0) { // rotate order of letters and push to array for (let i = 0; i < letters.length; i++) { @@ -164,9 +63,7 @@ function firstUnscramble(jumble) { let mixWord = mixletters.join(""); //join letters together into one scramble let revWord = revMix.join(""); - console.log("mixletters", mixletters); // firstMix.push(mixWord); // add scramble to results array - console.log("first", firstMix); // for (var j = 0; j < firstMix.length; j++) { // if (mixWord != firstMix[j]) { // firstMix.push(mixWord); //add scramble to final results @@ -174,58 +71,35 @@ function firstUnscramble(jumble) { // break; // } // } - console.log("final", finalMix); firstMix.push(mixWord); firstMix.push(revWord); - k = k - 2; - mixletters = []; // clear temp storage + k = k - 2; //reduce permutation counter + mixletters = []; // clear temp storages revMix = []; - l = l - 1; //reduce counter - console.log("l2", l); - console.log("k2", k); - console.log("letters", letters); + l = l - 1; //reduce counter let popped2 = letters.pop(); //remove last index - console.log("pop", popped2); let reworked2 = letters.splice(0, 0, popped2); //add last index to first spot mixWord = letters.join(""); - console.log("reworked", mixWord); - // firstMix.push(mixWord); // firstRun(mixWord); } else { - console.log("done"); - console.log("finalMix", finalMix); - console.log("firstMix", firstMix); - console.log("firstlength", firstMix.length); - console.log("finallength", finalMix.length); + console.log("done"); } }; - for (let i = 0; i <= letters.length; i++) { + for (let i = 0; i <= letters.length; i++) { //STARTING POINT if (n !== 0) { - let spliced = letters.splice(i, 1); - console.log("spliced", spliced); - let popped = spliced.pop(); - let reworked = letters.splice(0, 0, popped); - console.log("rework", letters); - // let redo = letters; - // console.log("redo", redo) - // firstRun(redo); - let mixWord = letters.join(""); - firstMix.push(mixWord); - n = n - 1; - let respliced = letters.splice(0, 1); + let spliced = letters.splice(i, 1); // take out one index + let popped = spliced.pop(); //remove string from that array + let reworked = letters.splice(0, 0, popped); //add removed to front of array + let mixWord = letters.join(""); //join into one string + firstMix.push(mixWord); //push to subsolution array + n = n - 1; // reduce counter + let respliced = letters.splice(0, 1); //rework array back to original state let popped2 = respliced.pop(); let fixed = letters.splice(i, 0, popped2); - console.log("n", n); - console.log("firstMix", firstMix); - } else { - console.log("first wave done"); - // const run1 = firstMix.length; - // console.log("run1", run1); - console.log("Kloop", k); + } else { //once done, begin pushing subsolution indexes through firstRun shuffle for (let i = 0; i < firstMix.length; i++) { - console.log("mixfirst", firstMix[i]); firstRun(firstMix[i]); } } @@ -233,3 +107,6 @@ function firstUnscramble(jumble) { } firstUnscramble("1234"); + + +