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; +} 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(''); +} diff --git a/chapter01/1.4 - PalinPerm/palinPerm.js b/chapter01/1.4 - PalinPerm/palinPerm.js index ca46496..9ee474f 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; + } +} diff --git a/chapter01/1.5 - OneAway/oneAway.js b/chapter01/1.5 - OneAway/oneAway.js index 1e66a89..795f580 100644 --- a/chapter01/1.5 - OneAway/oneAway.js +++ b/chapter01/1.5 - OneAway/oneAway.js @@ -61,4 +61,20 @@ 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; +} 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; + } +} 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);