From ee3104622fdea94ab552fabc35766967becacfa3 Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 24 Oct 2021 17:51:06 -0700 Subject: [PATCH 1/3] passes all tests for first two functions --- lib/exercises.js | 58 ++++++++++++++++++++++++++++++++++++++++-- test/exercises.test.js | 2 +- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/exercises.js b/lib/exercises.js index 8036868..4629481 100644 --- a/lib/exercises.js +++ b/lib/exercises.js @@ -3,18 +3,72 @@ // Time Complexity: ? // Space Complexity: ? function grouped_anagrams(strings) { - throw new Error("Method hasn't been implemented yet!"); + + var dic_anagrams = {}; + + for (let i = 0; i < strings.length; i++) { + var wordkey = strings[i].split('').sort().join(''); + if (wordkey in dic_anagrams) { + dic_anagrams[wordkey].push(strings[i]) + } + else { + dic_anagrams[wordkey] = [strings[i]] + } + } + + const output = [] + for (const [key, value] of Object.entries(dic_anagrams)) { + output.push(value) + } + 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) { - throw new Error("Method hasn't been implemented yet!"); + if (list.length < 1){ + return [] + } + var highest_freq = 0 + var hash_ele = {} + for (let i = 0; i highest_freq ){ + highest_freq = hash_ele[list[i]] + } + } + var counting_hash = {} + for (const [key, value] of Object.entries(hash_ele)) { + if (value in counting_hash) { + counting_hash[value].push(key) + } + else { + counting_hash[value] = [key] + } + } + + output = [] + while (output.length < k ){ + if (highest_freq in counting_hash){ + for (let i = 0; i < counting_hash[highest_freq].length; i++) { + if (output.length === k) { + continue + } + var number = counting_hash[highest_freq][i] + output.push(parseInt(number)); + } + } + highest_freq -= 1 + } +return output } + // This method will return true if the table is still // a valid sudoku table. // Each element can either be a ".", or a digit 1-9 diff --git a/test/exercises.test.js b/test/exercises.test.js index 4799720..691ab88 100644 --- a/test/exercises.test.js +++ b/test/exercises.test.js @@ -264,4 +264,4 @@ describe("exercises", function () { expect(valid).toEqual.false; }); }); -}); +}); \ No newline at end of file From 4ddf0ba7bab0da802b7867d6f249e1eda25f895f Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 24 Oct 2021 19:47:40 -0700 Subject: [PATCH 2/3] Passes all testes for hash-practice --- lib/exercises.js | 58 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/exercises.js b/lib/exercises.js index 4629481..488f8fb 100644 --- a/lib/exercises.js +++ b/lib/exercises.js @@ -30,41 +30,41 @@ function grouped_anagrams(strings) { // Time Complexity: ? // Space Complexity: ? function top_k_frequent_elements(list, k) { - if (list.length < 1){ + if (list.length < 1) { return [] } var highest_freq = 0 var hash_ele = {} - for (let i = 0; i highest_freq ){ - highest_freq = hash_ele[list[i]] + if (hash_ele[list[i]] > highest_freq) { + highest_freq = hash_ele[list[i]] } } var counting_hash = {} for (const [key, value] of Object.entries(hash_ele)) { if (value in counting_hash) { counting_hash[value].push(key) - } + } else { counting_hash[value] = [key] } } output = [] - while (output.length < k ){ - if (highest_freq in counting_hash){ - for (let i = 0; i < counting_hash[highest_freq].length; i++) { - if (output.length === k) { - continue - } + while (output.length < k) { + if (highest_freq in counting_hash) { + for (let i = 0; i < counting_hash[highest_freq].length; i++) { + if (output.length === k) { + continue + } var number = counting_hash[highest_freq][i] output.push(parseInt(number)); - } } - highest_freq -= 1 + } + highest_freq -= 1 } -return output + return output } @@ -77,9 +77,37 @@ return output // Time Complexity: ? // Space Complexity: ? function valid_sudoku(table) { - throw new Error("Method hasn't been implemented yet!"); + let rows = [[], [], [], [], [], [], [], [], []]; + let columns = [[], [], [], [], [], [], [], [], []]; + let boxes = [[], [], [], [], [], [], [], [], []]; + + for (let i = 0; i < 9; i++) { + for (let j = 0; j < 9; j++) { + + let cell = table[i][j]; + + if(cell !== ".") { + if (rows[i].includes(cell)) { + return false + } else rows[i].push(cell); + + if (columns[j].includes(cell)) { + return false; + } else columns[j].push(cell); + + let boxIndex = Math.floor((i / 3)) * 3 + Math.floor(j / 3); + + if (boxes[boxIndex].includes(cell)) { + return false; + } else boxes[boxIndex].push(cell); + + } + } + } + return true } + module.exports = { grouped_anagrams, top_k_frequent_elements, From bcb58275274e0abbc19f1d42a4dd13e10e3e221e Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 24 Oct 2021 20:02:07 -0700 Subject: [PATCH 3/3] added space and time complexity answers --- lib/exercises.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/exercises.js b/lib/exercises.js index 488f8fb..1461de7 100644 --- a/lib/exercises.js +++ b/lib/exercises.js @@ -1,7 +1,7 @@ // 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^2) +// Space Complexity: O(n) function grouped_anagrams(strings) { var dic_anagrams = {}; @@ -27,8 +27,8 @@ function grouped_anagrams(strings) { // 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^2) +// Space Complexity: O(n) function top_k_frequent_elements(list, k) { if (list.length < 1) { return [] @@ -74,8 +74,8 @@ 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: ? -// Space Complexity: ? +// Time Complexity: O(n^2) +// Space Complexity: O(n) function valid_sudoku(table) { let rows = [[], [], [], [], [], [], [], [], []]; let columns = [[], [], [], [], [], [], [], [], []];