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

Branches - Brianna #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 110 additions & 9 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
// This method will return an array of arrays.
// Each subarray will have strings which are anagrams of each other
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n * m log m) - N: number of strings passed in; M: sorting time of each string
// Space Complexity: O(n * m) - N: number of unique letter sets; M: number of total arrays stored
function grouped_anagrams(strings) {
Comment on lines +3 to 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work!

throw new Error("Method hasn't been implemented yet!");
let tracker = {}

for (let i = 0; i < strings.length; i++) {
let word = strings[i].split("").sort();

tracker[word] ? tracker[word].push(strings[i]) : tracker[word] = [strings[i]]
}

const keys = Object.keys(tracker);

const result = keys.map(key => { return tracker[key] });
return result
}

// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n + m) -> O(n) - N: length of list; m: unique numbers
// Space Complexity: O(m) m: unique numbers entered in hash
function top_k_frequent_elements(list, k) {
Comment on lines +22 to 24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work!

throw new Error("Method hasn't been implemented yet!");
if (list.length === 0) {
return []
}

let tracker = {};

for (let i = 0; i < list.length; i++) {
if (tracker[list[i]]) {
tracker[list[i]]++;
} else {
tracker[list[i]] = 1;
}
}


let result = [];

for (let i = 0; i < k; i++) {
let max = 0;
let maxKey = 0;

for (let key in tracker) {
if (tracker[key] > max) {
max = tracker[key]
maxKey = key;
delete tracker[key]
}
}

result.push(Number(maxKey))
}

return result
}


Expand All @@ -20,14 +63,72 @@ function top_k_frequent_elements(list, k) {
// Each element can either be a ".", or a digit 1-9
// The same digit cannot appear twice or more in the same
// row, column or 3x3 subgrid
// Time Complexity: ?
// Time Complexity: O(n*m) - n: rows in sudoku table; m: columns in sudoku table
// Space Complexity: ?

function valid_sudoku(table) {
Comment on lines -23 to 69

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really elegant solution! 👍

throw new Error("Method hasn't been implemented yet!");
}
let tracker = {
row: {},
column: {},
table: {}
};

for (let i = 0; i < table.length; i++) {
for (let j = 0; j < table.length; j++) {
const value = table[i][j]
if (value === '.') {
continue
}
const tableIndex = Math.floor((i / 3)) * 3 + j / 3

// tracker.table[tableIndex] = tracker.table[tableIndex] ? tracker.table[tableIndex] : {};
// tracker.row[i] = tracker.row[i] ? tracker.row[i] : {};
// tracker.column[j] = tracker.column[j] ? tracker.column[j] : {};
if (!tracker.table[tableIndex]) {
tracker.table[tableIndex] = {};
}
if (!tracker.row[i]) {
tracker.row[i]= {};
}
if (!tracker.column[j]) {
tracker.column[j] = {};
}

if (tracker.table[tableIndex][value] || tracker.row[i][value] || tracker.column[j][value]) {
return false;
}

tracker.table[tableIndex][value] = true;
tracker.row[i][value] = true;
tracker.column[j][value] = true;
}
}

// console.log(tracker)
return true;
};

module.exports = {
grouped_anagrams,
top_k_frequent_elements,
valid_sudoku
};


// console.log(grouped_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
// console.log(grouped_anagrams([]));
// console.log(grouped_anagrams(["eat", "ear", "tar", "pop", "pan", "pap"]));
// console.log(grouped_anagrams(["eat", "tae", "tea", "eta", "aet", "ate"]));
// console.log(grouped_anagrams(["tea","","eat","","tea",""]));
// console.log(top_k_frequent_elements([1,1,1,2,2,3], 2))
// console.log(top_k_frequent_elements([], 1))
console.log(valid_sudoku([
[".","8","7","6","5","4","3","2","1"],
["2",".",".",".",".",".",".",".","."],
["3",".",".",".",".",".",".",".","."],
["4",".",".",".",".",".",".",".","."],
["5",".",".",".",".",".",".",".","."],
["6",".",".",".",".",".",".",".","."],
["7",".",".",".",".",".",".",".","."],
["8",".",".",".",".",".",".",".","."],
["9",".",".",".",".",".",".",".","."]]))
Comment on lines +116 to +134

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave this out of a submission.

44 changes: 23 additions & 21 deletions test/exercises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ describe("exercises", function () {
it("will return [] for an empty array", function () {
// Arrange
const list = [];



// Act-Assert
expect(grouped_anagrams(list)).to.eql([]);
expect(grouped_anagrams(list)).toEqual([]);
});

it("will work for the README example", function () {
Expand All @@ -27,9 +28,9 @@ describe("exercises", function () {
];

// Assert
expect(answer.length).to.be.greaterThan(0);
expect(answer.length).toBeGreaterThan(0);
answer.forEach((array, index) => {
expect(array.sort()).to.eql(expected_answer[index]);
expect(array.sort()).toEqual(expected_answer[index]);
});
});

Expand All @@ -50,9 +51,9 @@ describe("exercises", function () {
];

// Assert
expect(answer.length).to.be.greaterThan(0);
expect(answer.length).toBeGreaterThan(0);
answer.forEach((array) => {
expect(expected_answer).to.deep.include(array.sort());
expect(expected_answer).toContainEqual(array.sort());
});
});

Expand All @@ -67,14 +68,14 @@ describe("exercises", function () {
];

// Assert
expect(answer.length).to.be.greaterThan(0);
expect(answer.length).toBeGreaterThan(0);
answer.forEach((array) => {
expect(expected_answer).to.deep.include(array.sort());
expect(expected_answer).toContainEqual(array.sort());
});
});
});

describe.skip("top_k_frequent_elements", function () {
describe("top_k_frequent_elements", function () {
it("works with example 1", function () {
// Arrange
const list = [1, 1, 1, 2, 2, 3];
Expand All @@ -84,7 +85,7 @@ describe("exercises", function () {
const answer = top_k_frequent_elements(list, k);

// Assert
expect(answer.sort()).to.eql([1, 2]);
expect(answer.sort()).toEqual([1, 2]);
});

it("works with example 2", function () {
Expand All @@ -96,7 +97,7 @@ describe("exercises", function () {
const answer = top_k_frequent_elements(list, k);

// Assert
expect(answer.sort()).to.eql([1]);
expect(answer.sort()).toEqual([1]);
});

it("will return [] for an empty array", function () {
Expand All @@ -108,7 +109,7 @@ describe("exercises", function () {
const answer = top_k_frequent_elements(list, k);

// Assert
expect(answer.sort()).to.eql([]);
expect(answer.sort()).toEqual([]);
});

it("will work for an array with k elements all unique", function () {
Expand All @@ -120,7 +121,7 @@ describe("exercises", function () {
const answer = top_k_frequent_elements(list, k);

// Assert
expect(answer.sort()).to.eql([1, 2, 3]);
expect(answer.sort()).toEqual([1, 2, 3]);
});

it("will work for an array when k is 1 and several elements appear 1 time (HINT Pick the 1st one)", function () {
Expand All @@ -132,11 +133,11 @@ describe("exercises", function () {
const answer = top_k_frequent_elements(list, k);

// Assert
expect(answer.sort()).to.eql([1]);
expect(answer.sort()).toEqual([1]);
});
});

describe.skip("valid sudoku", function () {
describe("valid sudoku", function () {
it("is not valid if a row has duplicate values", function () {
// Arrange
const table = [
Expand All @@ -155,7 +156,7 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.false;
expect(valid).toEqual.false;
});

it("is not valid if a column has duplicate values", function () {
Expand All @@ -176,7 +177,7 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.false;
expect(valid).toEqual.false;
});

it("works for the table given in the README", function () {
Expand All @@ -197,7 +198,7 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.true;
expect(valid).toEqual.true;
});

it("fails for the table given in the README", function () {
Expand All @@ -218,7 +219,7 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.false;
expect(valid).toEqual.false;
});

it("fails for a duplicate number in a sub-box", function () {
Expand All @@ -239,7 +240,7 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.false;
expect(valid).toEqual.false;
});

it("fails for a duplicate number in a bottom right sub-box", function () {
Expand All @@ -260,7 +261,8 @@ describe("exercises", function () {
const valid = valid_sudoku(table);

// Assert
expect(valid).to.be.false;
expect(valid).toEqual.false;
});
});
});