forked from AdaGold/Hashmap
-
Notifications
You must be signed in to change notification settings - Fork 47
Time - Angela #34
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
angethuy
wants to merge
3
commits into
Ada-C13:master
Choose a base branch
from
angethuy: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
Time - Angela #34
Changes from all commits
Commits
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,3 +1,17 @@ | ||
| # Takes two integer arrays as arguments and returns a new array of their intersecting numbers. | ||
| #Time complexity: O(n), worse case scenario we iterate over every item in both lists | ||
| #Space complexity: O(n), creating a new hash and a new array | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| integer_hash = {} | ||
|
|
||
| list1.each do |int| | ||
| integer_hash[int] = 1 | ||
| end | ||
|
|
||
| intersection = [] | ||
| list2.each do |int| | ||
| intersection << int if integer_hash.has_key?(int) | ||
| end | ||
|
|
||
| return intersection | ||
| end | ||
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,21 @@ | ||
| # Takes a string as an argument and checks if it could be a valid palindrome. | ||
| # Valid palindromes have a maximum of one letter that appears an odd number of times. | ||
| # Time: O(n), we iterate over the string once and over our hash once | ||
| # Space: O(n), worse case scenario we create a new hash collection that's as "large" as our string | ||
|
|
||
| def palindrome_permutation?(string) | ||
|
Comment on lines
+3
to
6
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. 👍 Nicely done. |
||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||
| hash = {} | ||
|
|
||
| string.chars.each do |letter| | ||
| hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1 | ||
| end | ||
|
|
||
| pivot_letters = 0 | ||
|
|
||
| hash.each do |k, v| | ||
| pivot_letters += 1 if v.odd? | ||
| return false if pivot_letters > 1 #catch early cases of finding more than 1 pivot letter | ||
| end | ||
|
|
||
| return true | ||
| end | ||
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,20 @@ | ||
| # Accepts two strings as arguments and checks if they're permutations of each other. | ||
| # Permutations share the same letter counts between each other. | ||
| # Time: O(n), we iterate through three different collections of fixed size | ||
| # Space: O(n), we create a new hash | ||
|
|
||
|
|
||
| def permutations?(string1, string2) | ||
|
Comment on lines
+1
to
7
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. 👍 nice work! |
||
| raise NotImplementedError, "permutations? not implemented" | ||
| hash = {} | ||
|
|
||
| string1.chars.each do |letter| | ||
| hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1 | ||
| end | ||
|
|
||
| string2.chars.each do |letter| | ||
| return false unless hash.has_key?(letter) && hash[letter] > 0 #quickly catch obvious fail cases | ||
| hash[letter] -= 1 | ||
| end | ||
|
|
||
| return !(hash.any? {|key, value| value > 0}) | ||
| end | ||
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.
👍 And correct time/space complexity