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 - Kelsey #1

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
85 changes: 73 additions & 12 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,78 @@
// 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) overall. Iterating over the array of strings to extract each string is O(n) where n is the length of the array. Alphabetizing each string is itself O(n) where n is the length of the word. Checking for the key in the uniqAnagrams object is O(1), and inserting an object into an array in Javascript is O(n). Pretty sure Object.keys is an O(n) situation as well.
// Space Complexity: O(n). Creating a hash with a size of up to 'n' length of strings, and creating an array of subarrays with a total size that's up to 'n' length of strings. (Also creating an array 'key' that's the size of the length of one string.)
function groupedAnagrams(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.

Good, but the lines are a bit long to read.

// return empty if no strings
if (strings.length === 0) return strings;

let uniqAnagrams = {};

strings.forEach(s => {
// alphabetize one string to use as a key
let key = s.split('');
key = key.sort();
key = key.join('');

if (uniqAnagrams.hasOwnProperty(key)) {
// if alphabetized string exists as a key
// add the un-alphabetized string to the key's array
uniqAnagrams[key].push(s)
} else {
// create a new key (alphabetized string) with value of an array containing the un-alphabetized string
uniqAnagrams[key] = [s];
}
})

let anagramArrays = [];
let keys = Object.keys(uniqAnagrams);

// for each key in uniqAnagrams,
// add the value (array of anagram strings) to anagramArrays
keys.forEach(key => {
anagramArrays.push(uniqAnagrams[key])
})

return anagramArrays;

}

// 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!");
// Time Complexity: O(n).
// Iterating over 'list' is O(n) where n is the length of 'list', iterating over 'uniqEls' is O(n) where n is the length of uniqEls(less than or equal to the length of 'list').
// Space Complexity: O(n). Creating a hash and an array are both O(n).

function topKFrequentElements(list, k) {
Comment on lines +42 to +46

Choose a reason for hiding this comment

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

Given your two nested loops you have an O(n^2) time complexity.

if (k === 0 || list.length === 0) return [];

// iterate over list (O(n))
// each unique item becomes a new key
// each repeated item adds the value of the key by 1
let listHash = {};
let uniqEls = [];
list.forEach(el => {
listHash[el] ? listHash[el] += 1 : listHash[el] = 1
if (!uniqEls.includes(el)) uniqEls.push(el)
})

// for each unique element,
// we sort based on their corresponding value
for (let i = 0; i <= uniqEls.length; i++) {
if (listHash[uniqEls[i]] < listHash[uniqEls[i+1]]) {
let j = i;
while (listHash[uniqEls[j]] < listHash[uniqEls[j + 1]]) {
let temp = uniqEls[j];
uniqEls[j] = uniqEls[j + 1];
uniqEls[j + 1] = temp;
j -= 1;
}
}
}

// return an array of the first 'k' elements from the unique elements array
if (uniqEls.length <= k) return uniqEls
return uniqEls.slice(0, k)
}


Expand All @@ -22,12 +83,12 @@ function top_k_frequent_elements(list, k) {
// 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
};
80 changes: 40 additions & 40 deletions test/exercises.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
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)).toEqual([]);
});

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"]
];

// 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(expectedAnswer[index]);
});
});

Expand All @@ -38,9 +38,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 @@ -50,9 +50,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(expectedAnswer).toContainEqual(array.sort());
});
});

Expand All @@ -61,30 +61,30 @@ 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);
expect(answer.length).toBeGreaterThan(0);
answer.forEach((array) => {
expect(expected_answer).to.deep.include(array.sort());
expect(expectedAnswer).toContainEqual(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]);
expect(answer.sort()).toEqual([1, 2]);
});

it("works with example 2", function () {
Expand All @@ -93,10 +93,10 @@ 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]);
expect(answer.sort()).toEqual([1]);
});

it("will return [] for an empty array", function () {
Expand All @@ -105,10 +105,10 @@ 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([]);
expect(answer.sort()).toEqual([]);
});

it("will work for an array with k elements all unique", function () {
Expand All @@ -117,10 +117,10 @@ 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]);
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 @@ -129,10 +129,10 @@ 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]);
expect(answer.sort()).toEqual([1]);
});
});

Expand All @@ -152,10 +152,10 @@ describe("exercises", function () {
];

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

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

it("is not valid if a column has duplicate values", function () {
Expand All @@ -173,10 +173,10 @@ describe("exercises", function () {
];

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

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

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

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

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

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

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

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

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

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

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

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

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

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