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

Amy W #3

Open
wants to merge 1 commit 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
37 changes: 28 additions & 9 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
// 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 input array

Choose a reason for hiding this comment

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

Since you're sorting the strings this would be O(n * m log m) where m is the average length of a string. If you can assume the strings are small, you can drop the m term.

// Space Complexity: O(nm) where n is the length of the input array and m is the average length of the values in the array
function groupedAnagrams(strings) {
const hashTable = {};

if (strings == false) {
return [];
}

strings.forEach(function(string) {
const arrayString = string.split('');
const sorted = arrayString.sort();
const sortedString = sorted.join();

if (hashTable[sortedString] == undefined) {
hashTable[sortedString] = [string]
} else {
hashTable[sortedString].push(string)
}
});

const output = Object.values(hashTable)
return output
}

// 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) {
function topKFrequentElements(list, k) {
throw new Error("Method hasn't been implemented yet!");
}

Expand All @@ -22,12 +41,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
};
36 changes: 18 additions & 18 deletions test/exercises.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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 answer = groupedAnagrams(list);
const expected_answer = [
["ate","eat","tea"],
["nat","tan"],
Expand All @@ -39,7 +39,7 @@ describe("exercises", function() {
const list = ["eat", "ear", "tar", "pop", "pan", "pap"];

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

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

// Act
const answer = grouped_anagrams(list);
const answer = groupedAnagrams(list);
const expected_answer = [
[ "aet", "ate", "eat", "eta", "tae", "tea"]
];
Expand All @@ -75,14 +75,14 @@ describe("exercises", function() {
});
});

describe.skip("top_k_frequent_elements", function() {
describe.skip("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,7 +130,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 @@ -153,7 +153,7 @@ describe("exercises", function() {
];

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

// Assert
expect(valid).toEqual(true);
Expand All @@ -174,7 +174,7 @@ describe("exercises", function() {
];

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

// Assert
expect(valid).toEqual(false);
Expand All @@ -195,7 +195,7 @@ describe("exercises", function() {
];

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

// Assert
expect(valid).toEqual(false);
Expand All @@ -216,7 +216,7 @@ describe("exercises", function() {
];

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

// Assert
expect(valid).toEqual(false);
Expand Down