forked from AdaGold/Hashmap
-
Notifications
You must be signed in to change notification settings - Fork 47
Space - Lee #35
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
theomoondev
wants to merge
22
commits into
Ada-C13:master
Choose a base branch
from
theomoondev: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
Space - Lee #35
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fdddac4
adds pseudocode
ad47667
defines intersection method, all tests passing
3fc73a8
fixes erroneous act statement in test 'returns true for 'pizza', 'pizza'
a76ff64
adds pseudocode
c771fab
amends pseudocode, checks if strings are equal or not of same length
779eac3
adds each char from string1 to the hash table
02451ba
looks up each char in string2 in the hash table, all tests passing
ef84fad
removes x from describe
509e6ae
adds pseudocode and comments on logic
ee12206
fixes name of test for 'hello', adds test for an even-length palindrome
cb8e169
adds each char in string to the hash table, works for even-length pal…
bb9764b
leverages collisions to check if the hash table is the expected lengt…
0fce2d8
modifies test for 'returns false if the number of a specific letter a…
a1c4796
fixes mislabeled it block and adds follow-up test for when the number…
f9ea6ec
fixes typo
1e93541
attempts to pass test 'returns false if the number of a specific lett…
95e8464
counts how many of each letter are in each string, compares hash tabl…
08d6564
refactors code, all tests passing
725f959
adds test and method for handling palindromes of any length with the …
4d20f68
removes extra space
0afdae4
simplifies code
db56aa4
fixes char count if letter only appears once in the string
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 @@ | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| list1.length < list2.length ? (smaller, larger = list1, list2) : (smaller, larger = list2, list1) # define which array is the smallest and which is the largest | ||
|
|
||
| # add each element from the smaller array to the hash table | ||
| lookup_hash = {} | ||
| smaller.each do |num| | ||
| lookup_hash[num] ? lookup_hash[num] = false : lookup_hash[num] = true | ||
| end | ||
|
|
||
| # lookup each element in the larger array in the hash table; add to results array if found | ||
| results = [] | ||
| larger.each do |num| | ||
| results << num if lookup_hash[num] | ||
| end | ||
|
|
||
| return results | ||
| 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,17 @@ | ||
|
|
||
| def palindrome_permutation?(string) | ||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||
| return true if string.empty? | ||
|
|
||
| # adds each char in string to the hash table | ||
| lookup_hash = {} | ||
| string.each_char do |char| | ||
| lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true | ||
| end | ||
|
|
||
| if lookup_hash.length == 1 # palindromes of any length that are made up of the same letter | ||
| return true | ||
| elsif string.length.even? # the palindrome should have two of each letter | ||
| return lookup_hash.length == string.length / 2 | ||
| elsif string.length.odd? # the palindrome should have only one odd letter out | ||
| return lookup_hash.length == (string.length / 2) + 1 | ||
| end | ||
| 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,25 @@ | ||
|
|
||
| def permutations?(string1, string2) | ||
|
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 think this could be simplified a bit, but otherwise well done. |
||
| raise NotImplementedError, "permutations? not implemented" | ||
| return true if string1 == string2 | ||
| return false if string1.length != string2.length | ||
|
|
||
| # add each char from string1 to the hash table | ||
| letter_count1 = {} | ||
| string1.each_char do |char| | ||
| letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 1 # counts how many of each letter are in string1 | ||
| end | ||
|
|
||
| # counts how many of each letter are in string2 | ||
| letter_count2 = {} | ||
| string2.each_char do |char| | ||
| letter_count2[char] ? letter_count2[char] += 1 : letter_count2[char] = 1 | ||
| end | ||
|
|
||
| return false if letter_count1 != letter_count2 # returns false if the number of a specific letter are different but the string lengths are the same (see comment in test file) | ||
|
|
||
| # lookup each char in string2 in the hash table | ||
| string2.each_char do |char| | ||
| return false if letter_count1[char].nil? | ||
| 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
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
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.
👍