Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update urlify.js #34

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
17 changes: 16 additions & 1 deletion chapter01/1.2 - Check Perm/checkPermute.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ console.log(checkPermute('aba', 'aab'), true);

console.log(checkPermute('aba', 'aaba'), false);

console.log(checkPermute('aba', 'aa'), false);
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;
}
17 changes: 17 additions & 0 deletions chapter01/1.3 - URLify/urlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}
19 changes: 18 additions & 1 deletion chapter01/1.4 - PalinPerm/palinPerm.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ var palinPerm = function(string) {

// TESTS
console.log(palinPerm('Tact Coa'), 'true');
console.log(palinPerm('Tact boa'), 'false');
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;
}
}
18 changes: 17 additions & 1 deletion chapter01/1.5 - OneAway/oneAway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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;
}
23 changes: 22 additions & 1 deletion chapter01/1.6 - String Compression/strComp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ var strComp = function(string) {

// Test
console.log('aaaaaa', strComp('aaaaaa'), 'a6');
console.log('aabcccccaaa', strComp('aabcccccaaa'), 'a2b1c5a3');
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;
}
}
49 changes: 48 additions & 1 deletion chapter01/1.7 - Rotate Matrix/rotateMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
*/


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