Skip to content

Commit 666850d

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 564dc72 + a6a9c89 commit 666850d

File tree

16 files changed

+961
-72
lines changed

16 files changed

+961
-72
lines changed
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
/*Given two strings, write a method to decide
22
if one is apermutation of the other permutation:*/
33

4-
const isPermutation = (str1, str2) =>{
4+
const isPermutation = (str1, str2) => {
55
if (str1.length !== str2.length) {
66
return false;
77
}
8-
const obj1 = determineCharCount(str1);
9-
const obj2 = determineCharCount(str2);
10-
for (let char of str1) {
11-
if (obj1[char] !== obj2[char]) {
8+
const freq1 = determineCharCount(str1);
9+
const freq2 = determineCharCount(str2);
10+
for (const [char, count] of freq1) {
11+
if (count !== freq2.get(char)) {
1212
return false;
1313
}
1414
}
1515
return true;
1616
//******** helper function ******
1717
function determineCharCount(string) {
18-
const obj = {};
19-
for (let char of string) {
20-
if (obj[char] !== undefined) {
21-
obj[char]++;
22-
} else {
23-
obj[char] = 1;
24-
}
25-
}
26-
return obj;
18+
return string
19+
.split('')
20+
.reduce(
21+
(acc, char) => acc.set(char, (acc.get(char) || 0) + 1),
22+
new Map()
23+
);
2724
}
28-
}
25+
};
2926

3027
// Tests:
31-
console.log(isPermutation('abc', 'abb') === false);
32-
console.log(isPermutation('abb', 'abc') === false);
33-
console.log(isPermutation('aaa', 'abc') === false);
34-
console.log(isPermutation('abc', 'abcd') === false);
35-
console.log(isPermutation('abc', 'bac') === true);
36-
console.log(isPermutation('', '') === true);
37-
console.log(isPermutation('12', '21') === true);
28+
console.log(isPermutation('abc', 'abb') === false);
29+
console.log(isPermutation('abb', 'abc') === false);
30+
console.log(isPermutation('aaa', 'abc') === false);
31+
console.log(isPermutation('abc', 'abcd') === false);
32+
console.log(isPermutation('abc', 'bac') === true);
33+
console.log(isPermutation('', '') === true);
34+
console.log(isPermutation('12', '21') === true);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Write a method to replace all spaces within a string with '%20'
22

3-
const URLify = (string) => string.split(' ').join('%20');
3+
const encodeSpaces = (string) => string.replace(/ /g, '%20');
44

55
// Tests:
6-
console.log(URLify('Hello World') === 'Hello%20World');
7-
console.log(URLify('') === '');
8-
console.log(URLify('This is an example') === 'This%20is%20an%20example');
6+
console.log(encodeSpaces('Hello World') === 'Hello%20World');
7+
console.log(encodeSpaces('') === '');
8+
console.log(encodeSpaces('This is an example') === 'This%20is%20an%20example');

JavaScript/chapter01/1.4 - PalinPerm/rroque98_sol.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Given a string, write a function to check if it is a permutation of a palindrome
22

33
const isPalindromePermutation = (str) => {
4-
const strNoSpaces = str.split(' ').join('');
5-
const obj = {};
4+
const strNoSpaces = str.replace(/ /g, '');
65
var oddCount = 0;
6+
const frequencies = new Map();
77
for (let char of strNoSpaces) {
8-
if (obj[char] !== undefined) {
9-
obj[char]++;
10-
} else {
11-
obj[char] = 1;
12-
}
8+
frequencies.set(char, 1 + (frequencies.get(char) || 0));
139
}
14-
for (let key in obj) {
15-
if (obj[key] % 2) {
16-
oddCount++;
10+
for (let frequency of frequencies.values()) {
11+
if (frequency % 2 === 0) {
12+
continue;
1713
}
14+
oddCount++;
1815
if (oddCount > 1) {
1916
return false;
2017
}

JavaScript/chapter01/1.5 - OneAway/rroque98_sol.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,69 @@ Given 2 strings write a function that will check if it is
44
1 or 0 edits away*/
55

66
const isOneAway = (str1, str2) => {
7+
const diffInLen = Math.abs(str1.length - str2.length);
8+
if (diffInLen > 1) {
9+
return false;
10+
}
11+
12+
let errorCount = 0;
713
if (str1.length === str2.length) {
8-
let errorCount = 0;
914
for (let i = 0; i < str1.length; i++) {
1015
if (str1[i] !== str2[i]) {
1116
errorCount++;
12-
}
13-
if (errorCount > 1) {
14-
return false;
17+
if (errorCount > 1) {
18+
return false;
19+
}
1520
}
1621
}
17-
} else {
18-
let errorCount = 0;
19-
const longestStr = findLongestStr(str1, str2);
20-
let x = 0;
21-
for(let i = 0; i < longestStr; i++) {
22-
if (str1[i] !== str2[x]) {
23-
errorCount++;
24-
x++;
25-
}
26-
if (errorCount > 1) {
27-
return false;
28-
}
29-
x++;
22+
return true;
23+
}
24+
const longStr = str1.length > str2.length ? str1 : str2;
25+
const shortStr = str1.length <= str2.length ? str1 : str2;
26+
for (let i = 0; i + errorCount < longStr.length; i++) {
27+
if (longStr[i + errorCount] === shortStr[i]) {
28+
continue;
3029
}
31-
// ***** Helper functions ********
32-
function findLongestStr(str1, str2) {
33-
str1 > str2 ? str1 : str2;
30+
errorCount++;
31+
if (errorCount > 1) {
32+
return false;
3433
}
3534
}
3635
return true;
36+
};
37+
38+
// ****** TESTS ******
39+
function runTests(cases, expected) {
40+
for (const [str1, str2] of cases) {
41+
console.log(
42+
isOneAway(str1, str2) === expected && isOneAway(str2, str1) === expected
43+
);
44+
}
3745
}
3846

39-
// TESTS
40-
console.log(isOneAway('pale', 'ple') === true);
41-
console.log(isOneAway('pales', 'pale') === true);
42-
console.log(isOneAway('pale', 'bale') === true);
43-
console.log(isOneAway('pale', 'bake') === false);
47+
runTests(
48+
[
49+
['pale', 'ple'], // deletion
50+
['pale', 'opale'], // insertion in beginning
51+
['pale', 'palse'], // insertion in middle
52+
['pale', 'pales'], // insertion at end
53+
['pale', 'bale'], // replacement
54+
['p', 'b'],
55+
['p', 'p'],
56+
['p', ''],
57+
['', '']
58+
],
59+
true
60+
);
61+
62+
runTests(
63+
[
64+
['pale', 'ae'], // greater than 1 deletions
65+
['pale', 'ppalpe'], // greater than 1 insertions
66+
['pale', 'bake'], // greater than 1 replacements
67+
['pale', 'balpe'], // 1 insertion, 1 replacement
68+
['pale', 'plo'], // 1 deletion, 1 replacement
69+
['pale', 'ales'] // 1 deletion, 1 insertion
70+
],
71+
false
72+
);
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
/* Implement a method to perform basic string
2-
compression using the counts of repeated characters
1+
/* Implement a method to perform basic string
2+
compression using the counts of repeated characters.
3+
If the compressed string length is more than original
4+
string length, return original string.
35
Ex: 'aabcccccaaa' would become a2b1c5a3
46
*/
57

68
const stringCompression = (str) => {
79
if (!str.length) {
810
return '';
911
}
10-
var compStr = '';
11-
var count = 1;
12-
var currentChar = str[0];
13-
for (let i = 1; i < str.length; i++) {
12+
let compStr = '';
13+
let count = 0;
14+
let currentChar = str[0];
15+
for (let i = 0; i < str.length; i++) {
1416
let char = str[i];
1517
if (char === currentChar) {
1618
count++;
17-
if (i === str.length - 1) {
18-
compStr += `${currentChar}${count}`;
19-
}
2019
} else {
2120
compStr += `${currentChar}${count}`;
2221
currentChar = char;
2322
count = 1;
2423
}
2524
}
25+
compStr += `${currentChar}${count}`;
26+
if (compStr.length > str.length) {
27+
return str;
28+
}
2629
return compStr;
2730
}
2831

2932
// TESTS
3033
console.log(stringCompression('aabcccccaaa') === 'a2b1c5a3');
3134
console.log(stringCompression('cccccccc') === 'c8');
3235
console.log(stringCompression('') === '');
33-
console.log(stringCompression('AabccCccaaa') === 'A1a1b1c2C1c2a3');
36+
console.log(stringCompression('AabccCccaaa') === 'AabccCccaaa');
37+
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
38+
console.log(stringCompression('x') === 'x');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*Given an image represented by an N*N matrix where
2+
each pixel in the image is 4 bytes, write a method
3+
to rotate the image by 90 degrees. Can you do this
4+
in place?*/
5+
6+
const rotateImage = nestedArr => {
7+
const n = nestedArr.length;
8+
if (n === 0 || n === 1) {
9+
return nestedArr;
10+
}
11+
var rotatedArr = [];
12+
for (let col = 0; col < n; col++) {
13+
let newRow = [];
14+
for (let row = n - 1; row >= 0; row--) {
15+
newRow.push(nestedArr[row][col]);
16+
}
17+
rotatedArr.push(newRow);
18+
}
19+
return rotatedArr;
20+
};
21+
22+
// TESTS:
23+
console.log(
24+
JSON.stringify(rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) ===
25+
JSON.stringify([[7, 4, 1], [8, 5, 2], [9, 6, 3]])
26+
);
27+
console.log(JSON.stringify(rotateImage([[1]])) === JSON.stringify([[1]]));
28+
console.log(JSON.stringify(rotateImage([[]])) === JSON.stringify([[]]));
29+
console.log(JSON.stringify(rotateImage([])) === JSON.stringify([]));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*Write an algorithm such that if an element
2+
in an M*N matrix is 0,it's entire row and column
3+
are set to 0*/
4+
5+
const zeroMatrix = nestedArr => {
6+
const rowsAndColsWithZeros = checkForZeroIndex(nestedArr);
7+
const rowLength = nestedArr[0].length;
8+
const columnLength = nestedArr.length;
9+
for (const row in rowsAndColsWithZeros['rows']) {
10+
for (let col = 0; col < rowLength; col++) {
11+
nestedArr[row][col] = 0;
12+
}
13+
}
14+
for (const col in rowsAndColsWithZeros['columns']) {
15+
for (let row = 0; row < columnLength; row++) {
16+
nestedArr[row][col] = 0;
17+
}
18+
}
19+
return nestedArr;
20+
};
21+
22+
// **** Helper function ****
23+
function checkForZeroIndex(nestArr) {
24+
let zeroIndices = { rows: {}, columns: {} };
25+
for (let row = 0; row < nestArr.length; row++) {
26+
for (let col = 0; col < nestArr[row].length; col++) {
27+
if (nestArr[row][col] === 0) {
28+
zeroIndices['rows'][row] = true;
29+
zeroIndices['columns'][col] = true;
30+
}
31+
}
32+
}
33+
return zeroIndices;
34+
}
35+
36+
// **** TESTS ****:
37+
let actual = zeroMatrix([[]]);
38+
let expected = [[]];
39+
isEqual(actual, expected);
40+
41+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
42+
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
43+
isEqual(actual, expected);
44+
45+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
46+
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
47+
isEqual(actual, expected);
48+
49+
actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
50+
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
51+
isEqual(actual, expected);
52+
53+
function isEqual(actual, expected) {
54+
console.log(JSON.stringify(actual) === JSON.stringify(expected));
55+
}
56+
57+
// ****HELPER FUNCTION TESTS ****:
58+
actual = checkForZeroIndex([[1, 2, 0]]);
59+
expected = { rows: { 0: true }, columns: { 2: true } };
60+
testCheckForZeroIndex(actual, expected);
61+
62+
actual = checkForZeroIndex([[0]]);
63+
expected = { rows: { 0: true }, columns: { 0: true } };
64+
testCheckForZeroIndex(actual, expected);
65+
66+
actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
67+
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
68+
testCheckForZeroIndex(actual, expected);
69+
70+
function testCheckForZeroIndex(actual, expected) {
71+
let rows = JSON.stringify(actual.rows);
72+
let cols = JSON.stringify(actual.columns);
73+
let expRows = JSON.stringify(expected.rows);
74+
let expCols = JSON.stringify(expected.columns);
75+
console.log(rows === expRows && cols === expCols);
76+
}

Python/chapter01/1.3 - URLify/camilo_solution_1.3.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
(Note: If implementing in Java,please use a character array so that you can
66
perform this operation in place.)
77
EXAMPLE
8-
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
8+
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
99
"""
1010

1111
def urlify(str):
12+
len_ = len(str) #gets the lenght of str
13+
new_str = ""#build a new string
14+
for i in range(len_-1):
15+
if str[i] == " " and str[i+1]==" ":#if two space aheah are space then break
16+
break
17+
if str[i] == " ": #if we find a space just add %20
18+
new_str+= "%20"
19+
20+
else: #just add the chars
21+
new_str+= str[i]
22+
23+
return new_str
24+
25+
print(urlify("MY house is on fire "))

0 commit comments

Comments
 (0)