From d63e064db119b6ca472f949fe0affac0dc368e88 Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 12:43:21 -0500 Subject: [PATCH 1/9] Update urlify.js --- chapter01/1.3 - URLify/urlify.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/chapter01/1.3 - URLify/urlify.js b/chapter01/1.3 - URLify/urlify.js index 7e993d5..17f4bb4 100644 --- a/chapter01/1.3 - URLify/urlify.js +++ b/chapter01/1.3 - URLify/urlify.js @@ -22,3 +22,20 @@ var urlify = function(str, length) { }; console.log(urlify('Mr John Smith ', 13), 'Mr%20John%20Smith'); + +// ONE MORE SOLUTION + +function urlify(string, length) { + var arr = string.split(''); + for ( char of arr ) { + if ( char === ' ') { + //replacing ' ' with '%20' + var update = arr.splice(arr.indexOf(char), 1, '%20'); + } + } + if (string.length !== length) { + var remove = (string.length - length); + arr.splice ( - 1 * remove); + } + return arr.join(''); +} From f2b5cc77d9198b87d5903b2f30c74884a72b99ad Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 14:12:05 -0500 Subject: [PATCH 2/9] Update checkPermute.js --- chapter01/1.2 - Check Perm/checkPermute.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/chapter01/1.2 - Check Perm/checkPermute.js b/chapter01/1.2 - Check Perm/checkPermute.js index 0cb0931..20d8952 100644 --- a/chapter01/1.2 - Check Perm/checkPermute.js +++ b/chapter01/1.2 - Check Perm/checkPermute.js @@ -16,4 +16,19 @@ console.log(checkPermute('aba', 'aab'), true); console.log(checkPermute('aba', 'aaba'), false); -console.log(checkPermute('aba', 'aa'), false); \ No newline at end of file +console.log(checkPermute('aba', 'aa'), false); + +//ANOTHER SOLUTION + +function permutation(str1, str2) { + if (str1.length !== str2.length) { + return false; + } + + for (var i = 0; i < str1.length; i++) { + if (!str2.includes(str1.charAt(i))) { + return false; + } + }; + return true; +} From 7f1137a602ff1c48a0d1af69e891ee3d2d33e69b Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 14:30:24 -0500 Subject: [PATCH 3/9] Update palinPerm.js --- chapter01/1.4 - PalinPerm/palinPerm.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/chapter01/1.4 - PalinPerm/palinPerm.js b/chapter01/1.4 - PalinPerm/palinPerm.js index ca46496..806ec6a 100644 --- a/chapter01/1.4 - PalinPerm/palinPerm.js +++ b/chapter01/1.4 - PalinPerm/palinPerm.js @@ -31,4 +31,21 @@ var palinPerm = function(string) { // TESTS console.log(palinPerm('Tact Coa'), 'true'); -console.log(palinPerm('Tact boa'), 'false'); \ No newline at end of file +console.log(palinPerm('Tact boa'), 'false'); + +//another solution + +function palindrom(string) { + var test = []; + var arr = string.split(''); + for (char of arr) { + if (char !== ' ') { + test.push(char.toLowerCase()); + } + } + if (test.join('') === test.reverse().join('')) { + return true; + } else { + return false; + } +} From e9abf1ae5aa97daf257a54967e7740cede82fd1c Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 14:32:08 -0500 Subject: [PATCH 4/9] Adding a new solution --- chapter01/1.4 - PalinPerm/palinPerm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter01/1.4 - PalinPerm/palinPerm.js b/chapter01/1.4 - PalinPerm/palinPerm.js index 806ec6a..9ee474f 100644 --- a/chapter01/1.4 - PalinPerm/palinPerm.js +++ b/chapter01/1.4 - PalinPerm/palinPerm.js @@ -33,7 +33,7 @@ var palinPerm = function(string) { console.log(palinPerm('Tact Coa'), 'true'); console.log(palinPerm('Tact boa'), 'false'); -//another solution +//Another solution function palindrom(string) { var test = []; From ef11e70fe4c644924cbf323dd92d02ab35155218 Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 15:29:45 -0500 Subject: [PATCH 5/9] Update oneAway.js --- chapter01/1.5 - OneAway/oneAway.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/chapter01/1.5 - OneAway/oneAway.js b/chapter01/1.5 - OneAway/oneAway.js index 1e66a89..f77c5b9 100644 --- a/chapter01/1.5 - OneAway/oneAway.js +++ b/chapter01/1.5 - OneAway/oneAway.js @@ -61,4 +61,21 @@ var oneAway = function(string1, string2) { console.log(oneAway('pale', 'ple'), true); console.log(oneAway('pales', 'pale'), true); console.log(oneAway('pale', 'bale'), true); -console.log(oneAway('pale', 'bake'), false); \ No newline at end of file +console.log(oneAway('pale', 'bake'), false); + + +//Another solution + +function oneAway(str1, str2) { + var counter = 0; + for (var i = 0; i < str1.length; i++) { + if (!str2.includes(str1.charAt(i))) { + counter++; + } + }; + + if (counter > 1) { + return false; + } + return true; +} From 9338283d8e59ffd6ae958625d536bc9c86ff4bf0 Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 15:31:41 -0500 Subject: [PATCH 6/9] Update oneAway.js --- chapter01/1.5 - OneAway/oneAway.js | 1 - 1 file changed, 1 deletion(-) diff --git a/chapter01/1.5 - OneAway/oneAway.js b/chapter01/1.5 - OneAway/oneAway.js index f77c5b9..795f580 100644 --- a/chapter01/1.5 - OneAway/oneAway.js +++ b/chapter01/1.5 - OneAway/oneAway.js @@ -65,7 +65,6 @@ console.log(oneAway('pale', 'bake'), false); //Another solution - function oneAway(str1, str2) { var counter = 0; for (var i = 0; i < str1.length; i++) { From 16696d1c0dae044db4003a14e05f5bf5a291bb1e Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 11 Dec 2018 17:14:39 -0500 Subject: [PATCH 7/9] Update strComp.js --- chapter01/1.6 - String Compression/strComp.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/chapter01/1.6 - String Compression/strComp.js b/chapter01/1.6 - String Compression/strComp.js index 2d0e776..18095de 100644 --- a/chapter01/1.6 - String Compression/strComp.js +++ b/chapter01/1.6 - String Compression/strComp.js @@ -22,4 +22,25 @@ var strComp = function(string) { // Test console.log('aaaaaa', strComp('aaaaaa'), 'a6'); -console.log('aabcccccaaa', strComp('aabcccccaaa'), 'a2b1c5a3'); \ No newline at end of file +console.log('aabcccccaaa', strComp('aabcccccaaa'), 'a2b1c5a3'); + +//Another solution + +function compression(str) { + var counter = 1; + var result = ''; + for (var i = 0; i < str.length; i++) { + if (str.charAt(i) === str.charAt(i + 1)) { + counter+=1; + } else { + result += str.charAt(i) + counter.toString(); + counter = 1; + } + } + + if (result.length < str.length ) { + return result; + } else { + return str; + } +} From 4c14a17a0648c4000edcbb7f1019af3a0743533c Mon Sep 17 00:00:00 2001 From: Mary Date: Thu, 13 Dec 2018 12:11:45 -0500 Subject: [PATCH 8/9] Update rotateMatrix.js --- chapter01/1.7 - Rotate Matrix/rotateMatrix.js | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/chapter01/1.7 - Rotate Matrix/rotateMatrix.js b/chapter01/1.7 - Rotate Matrix/rotateMatrix.js index b0e3e28..eaaf539 100644 --- a/chapter01/1.7 - Rotate Matrix/rotateMatrix.js +++ b/chapter01/1.7 - Rotate Matrix/rotateMatrix.js @@ -105,4 +105,51 @@ ii) for each row, iterate pixels until edge - 1 (pixel at edge would have been transformed by the first pixel) iii) at each pixel iteration, iterate through 4 sides iv) do iteration in place, i.e. store a temp pixel for moving things around -*/ \ No newline at end of file +*/ + + +//creating a matrix +function createMatrix(cols, rows) { + const arr = new Array(cols); + for (let i = 0; i < arr.length; i++) { + arr[i] = new Array(rows); + for (let j = 0; j < arr[i].length; j++) { + arr[i][j] = Math.round(Math.random(10) * 10); + } + } + return arr; +} + +//getting a new I +function getRotatedI(paramJ) { + return paramJ; +} + +//getting a new J +function getRotatedJ(arrLength, paramI) { + return arrLength - 1 - paramI; +} + +//rotating a matrix +function rotate(matrixSource) { + console.log(matrixSource); + const newMatrix = new Array(matrixSource.length); + for (let i = 0; i < newMatrix.length; i++) { + newMatrix[i] = new Array(newMatrix.length); + for (let j = 0; j < newMatrix[i].length; j++) { + newMatrix[i][j] = false; + } + } + + for (let i = 0; i < matrixSource.length; i ++) { + for (let j = 0; j < matrixSource[i].length; j++) { + const rotatedJ = getRotatedJ(matrixSource[i].length, i); + const rotatedI = getRotatedI(j); + newMatrix[rotatedI][rotatedJ] = matrixSource[i][j]; + console.log(newMatrix); + } + } +} + +const myMatrix = createMatrix(2, 2); +rotate(myMatrix); From d19d79018a90411164b2259cb6a5a1c68f12ac62 Mon Sep 17 00:00:00 2001 From: Mary Date: Thu, 13 Dec 2018 12:21:16 -0500 Subject: [PATCH 9/9] Update rotateMatrix.js