forked from mmcknett/hash-practice-js
-
Notifications
You must be signed in to change notification settings - Fork 8
Wrote grouped_anagrams method. #1
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
Open
dev-elle-up
wants to merge
4
commits into
Ada-C11:master
Choose a base branch
from
dev-elle-up:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
021a256
Wrote grouped_anagrams method.
dev-elle-up ffd2d27
Added time and space complexity notes for anagrams method. Updated te…
dev-elle-up f563ff9
Changed snake case to camel case for JS convention.
dev-elle-up b6e3d22
QUESTIONS IN THIS COMMIT. Wrote pseudocode for k_top_elements. Pontif…
dev-elle-up File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?***) | ||
| 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]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] | ||
|
|
@@ -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]); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -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"], | ||
|
|
@@ -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()); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -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]); | ||
|
|
@@ -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]); | ||
|
|
@@ -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([]); | ||
|
|
@@ -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]); | ||
|
|
@@ -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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [ | ||
|
|
@@ -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() { | ||
|
|
@@ -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() { | ||
|
|
@@ -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() { | ||
|
|
@@ -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; | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.