Skip to content
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
76 changes: 62 additions & 14 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
// This method will return an array of arrays.
// Each subarray will have strings which are anagrams of each other
// Time Complexity: ?
// Space Complexity: ?
function grouped_anagrams(strings) {
throw new Error("Method hasn't been implemented yet!");
// Time Complexity: O(n) where n is the length of the array passed in as the argument "strings" because each item in the array is visited once
// Space Complexity: O(1) (***Chris, is this right? It takes 2 times the space of 'strings' which reduces to 1. Or is it O(n) because the space taken is is 2 x n where n is the length of the 'strings' array?***)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would say O(n) because you are building a JavaScript object which scales based on the size of the array.

function groupedAnagrams(strings) {
let anagrams = {};

strings.forEach(str => {
let tempStr = str.split('').sort().join(''); // alphabetize str
anagrams[tempStr] ? anagrams[tempStr].push(str) : anagrams[tempStr] = [str];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of a ternary.

});

return Object.values(anagrams);
}

// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// 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: ?
function top_k_frequent_elements(list, k) {
throw new Error("Method hasn't been implemented yet!");

// Clarifying questions:
// 1) What happens if there are < k elements in the list array?
// 2) How big are list and k likely to be? (Optimize for list or for k)?
// 3) Are the elements in list likely to be anything that can't be used as a key? (Will they always be numbers/strings/something that can be coorced into a string? Will they ever be null or undefined?)
function topKFrequentElements(list, k) {
if (list.length < k){
return list;
} // this code might not be necessary once the function is complete

let elementCounts = {};

// iterate through each item in list (array)
// if the item is a key in elementCounts, add 1 to is value
// if it's not a key already, add it as a key with value 1

// find highest count in arrary
const maxCount = elementCounts.values.max;
let solutions = [];

obj = { name: 'Bobo' }
obj.somethingElse = obj.name
delete obj.name

// OPTION 1
// k.times { // *** O(k) --> O(1) ?
// loop through the list array // *** O(n*k)
// if the item's matching value === maxCount, add the key to solutions array
// update the maxCount to the next item in the array
// }


// OPTION 2
// const eleCountsInverted = a new object switching keys and values // *** O(n) ?
// now counts are keys, values are the items from the list array
let highestCountsArray = Object.keys(eleCountsInverted); // O(1) ?
// k.times {
// loop through highestCountsArray // *** O(k) --> O(1) ?
// add the values associated with these keys to the solutions array
// }

// return an array with the first k keys with highest values in any order
return solutions;

}


// This method will return the true if the table is still
// a valid sudoku table.
// This method will return true if the table is a valid sudoku table.
// 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: ?
// Space Complexity: ?
function valid_sudoku(table) {
function validSudoku(table) {
throw new Error("Method hasn't been implemented yet!");
}

module.exports = {
grouped_anagrams,
top_k_frequent_elements,
valid_sudoku
groupedAnagrams,
topKFrequentElements,
validSudoku
};
100 changes: 71 additions & 29 deletions test/exercises.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const expect = require('chai').expect;
const {
grouped_anagrams,
top_k_frequent_elements,
valid_sudoku
groupedAnagrams,
topKFrequentElements,
validSudoku
} = require('../lib/exercises');

describe("exercises", function() {
describe("grouped_anagrams", function() {
describe("groupedAnagrams", function() {
it("will return [] for an empty array", function() {
// Arrange
const list = [];

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

it("will work for the README example", function() {
// Arrange
const list = ["eat", "tea", "tan", "ate", "nat", "bat"];

// Act
const answer = grouped_anagrams(list);
const expected_answer = [
const answer = groupedAnagrams(list);
const expectedAnswer = [
["ate","eat","tea"],
["nat","tan"],
["bat"]
Expand All @@ -30,7 +30,7 @@ describe("exercises", function() {
// Assert
expect(answer.length).to.be.greaterThan(0);
answer.forEach((array, index) => {
expect(array.sort()).to.eql(expected_answer[index]);
expect(array.sort()).to.eql(expectedAnswer[index]);
});
});

Expand All @@ -39,9 +39,9 @@ describe("exercises", function() {
const list = ["eat", "ear", "tar", "pop", "pan", "pap"];

// Act
const answer = grouped_anagrams(list);
const answer = groupedAnagrams(list);

const expected_answer = [
const expectedAnswer = [
["eat"],
["ear"],
["tar"],
Expand All @@ -53,7 +53,7 @@ describe("exercises", function() {
// Assert
expect(answer.length).to.be.greaterThan(0);
answer.forEach((array) => {
expect(expected_answer).to.deep.include(array.sort());
expect(expectedAnswer).to.deep.include(array.sort());
});
});

Expand All @@ -62,27 +62,27 @@ describe("exercises", function() {
const list = ["eat", "tae", "tea", "eta", "aet", "ate"]

// Act
const answer = grouped_anagrams(list);
const expected_answer = [
const answer = groupedAnagrams(list);
const expectedAnswer = [
[ "aet", "ate", "eat", "eta", "tae", "tea"]
];

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

describe.skip("top_k_frequent_elements", function() {
describe("topKFrequentElements", function() {
it("works with example 1", function() {
// Arrange
const list = [1,1,1,2,2,3];
const k = 2;

// Act
const answer = top_k_frequent_elements(list, k);
const answer = topKFrequentElements(list, k);

// Assert
expect(answer.sort()).to.eql([1,2]);
Expand All @@ -94,7 +94,7 @@ describe("exercises", function() {
const k = 1;

// Act
const answer = top_k_frequent_elements(list, k);
const answer = topKFrequentElements(list, k);

// Assert
expect(answer.sort()).to.eql([1]);
Expand All @@ -106,7 +106,7 @@ describe("exercises", function() {
const k = 1;

// Act
const answer = top_k_frequent_elements(list, k);
const answer = topKFrequentElements(list, k);

// Assert
expect(answer.sort()).to.eql([]);
Expand All @@ -118,7 +118,7 @@ describe("exercises", function() {
const k = 3;

// Act
const answer = top_k_frequent_elements(list, k);
const answer = topKFrequentElements(list, k);

// Assert
expect(answer.sort()).to.eql([1, 2, 3]);
Expand All @@ -130,14 +130,56 @@ describe("exercises", function() {
const k = 1;

// Act
const answer = top_k_frequent_elements(list, k);
const answer = topKFrequentElements(list, k);

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

describe.skip("valid sudoku", function() {
it("is not valid if a row has duplicate values", function() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'll add this to my set of tests

// Arrange
const table = [
["5","3",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".","5","5",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."]
];

// Act
const valid = validSudoku(table);

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

it("is not valid if a column has duplicate values", function() {
// Arrange
const table = [
["5",".",".",".",".",".",".",".","."],
["2",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".","4",".",".",".","."],
[".",".",".",".","4",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."]
];

// Act
const valid = validSudoku(table);

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

it("works for the table given in the README", function() {
// Arrange
const table = [
Expand All @@ -153,10 +195,10 @@ describe("exercises", function() {
];

// Act
const valid = valid_sudoku(table);
const valid = validSudoku(table);

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

it("fails for the table given in the README", function() {
Expand All @@ -174,10 +216,10 @@ describe("exercises", function() {
];

// Act
const valid = valid_sudoku(table);
const valid = validSudoku(table);

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

it("fails for a duplicate number in a sub-box", function() {
Expand All @@ -195,10 +237,10 @@ describe("exercises", function() {
];

// Act
const valid = valid_sudoku(table);
const valid = validSudoku(table);

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

it("fails for a duplicate number in a bottom right sub-box", function() {
Expand All @@ -216,10 +258,10 @@ describe("exercises", function() {
];

// Act
const valid = valid_sudoku(table);
const valid = validSudoku(table);

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