forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 40
Faiza #17
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
Faiza1987
wants to merge
6
commits into
Ada-C11:master
Choose a base branch
from
Faiza1987: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
Faiza #17
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ccf4ff
Added code for grouped anagrams and top kth element
Faiza1987 0a00a9f
Added code for grouped_anagrams and to kth
Faiza1987 3851c3e
Modified code for top kth element
Faiza1987 2570683
added original way I did the grouped anagramns problem without the hash
Faiza1987 9b8e888
Unskipped some tests and modiefied some code for grouped anagrams
Faiza1987 a5917db
Added Space/Time Complexity for top_kth_elements
Faiza1987 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,27 +1,105 @@ | ||
|
|
||
|
|
||
| # 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) because it will need to check every item in the array | ||
| # Space Complexity: O(1) | ||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| if strings.length == 0 | ||
| return [] | ||
| end | ||
|
|
||
| hash = Hash.new | ||
| temp_word = "" | ||
|
|
||
| # in a loop | ||
| # split the word at ("") and sort it and save the results into an array (if 2 words are anagrams of each other, sorting them means the letters of both words will be re-arranged in the same sequence) | ||
| strings.each do |word| | ||
| temp_word = word.split("").sort | ||
|
|
||
| # check if the hash has the element of the array as a key, if not then add it as a key in the hash | ||
| if hash.include?(temp_word) | ||
| hash[temp_word].push(word) | ||
| # check if the 2nd element is in the hash, if not then add it as a value to the key that was previously added | ||
| else | ||
| hash[temp_word] = [word] | ||
| end | ||
| end | ||
|
|
||
| # puts "Hash" | ||
| # puts hash | ||
|
|
||
| # save the values of the hash into an array and return that array | ||
| anagrams = [] | ||
| hash.each do |key, value| | ||
| anagrams.push(value) | ||
| end | ||
| return anagrams | ||
| end | ||
|
|
||
| # def grouped_anagrams(list) | ||
| # if list.length == 0 | ||
| # return [] | ||
| # end | ||
|
|
||
| # parent = [[list[0]]] | ||
|
|
||
| # for i in (1...list.length) | ||
| # new_word_srtd = list[i].chars.sort.join | ||
| # is_anagram = false | ||
|
|
||
| # for j in (0...parent.length) | ||
| # old_words_srtd = parent[j][0].chars.sort.join | ||
| # if old_words_srtd == new_word_srtd | ||
| # is_anagram = true | ||
| # parent[j].push(list[i]) | ||
| # break | ||
| # end | ||
| # end | ||
| # if is_anagram == false | ||
| # parent.push([list[i]]) | ||
| # end | ||
| # end | ||
| # return parent | ||
| # end | ||
|
|
||
| # 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) because it will have to loop throught the entire array | ||
| # Space Complexity: O(1) | ||
|
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. What about the space complexity of the hash? |
||
| def top_k_frequent_elements(list, k) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
|
|
||
| if list.length == 0 | ||
| return [] | ||
| end | ||
|
|
||
| hash = Hash.new | ||
| # loop through the list | ||
| # add each element in the list as a key in k with a count value (to count how many times that particular element exists in the list) | ||
| # if element doesn't exist in the hash, then add it with a default value of 1 | ||
|
|
||
| list.each do |element| | ||
| if hash.include?(element) | ||
| hash[element] += 1 | ||
| else | ||
| hash[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| # in the hash, we can sort the key value pairs by order of occurence (most occurrences first) | ||
| # we can push the highest occurening keys into an array and return the array | ||
|
|
||
| top_k = [] | ||
| hash.sort_by { |key, value| -value } | ||
| hash.each do |key, value| | ||
| top_k << key | ||
| end | ||
| return top_k[0..(k - 1)] | ||
| end | ||
|
|
||
|
|
||
| # This method will return the true if the table is still | ||
| # 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 | ||
| # The same digit cannot appear twice or more in the same | ||
| # row, column or 3x3 subgrid | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
|
|
||
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
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,4 +1,4 @@ | ||
|
|
||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require 'minitest/reporters' | ||
|
|
||
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.
Since you're building a hash, the space complexity is O(n)