-
Notifications
You must be signed in to change notification settings - Fork 47
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
Space - Charlotte #30
base: master
Are you sure you want to change the base?
Changes from all commits
61c663e
bec43d8
4de765c
1867f57
7c3e08c
f34d950
8d37d50
6f9051b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
def intersection(list1, list2) | ||
raise NotImplementedError, "Intersection not implemented" | ||
end | ||
array = [] | ||
nums = {} | ||
i = 0 | ||
|
||
while list1.length > i | ||
# creating nums hash with number as key and value as true | ||
nums[list1[i]] = true | ||
i += 1 | ||
end | ||
|
||
list2.each do |element| | ||
# the value at nums[element] is going to be nil or true | ||
# check if list2[element] is equal to a key in the nums hash | ||
# if it is equal, push element into array b/c that is an intersection. | ||
array << element if nums[element] | ||
end | ||
array | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,21 @@ | ||
|
||
def palindrome_permutation?(string) | ||
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. 👍 |
||
raise NotImplementedError, "palindrome_permutation? not implemented" | ||
end | ||
return true if string.empty? | ||
|
||
# create a new hash which will hold counts of characters | ||
# set the default value of all keys to 0 | ||
letters = Hash.new(0) | ||
string.each_char do |char| | ||
letters[char] += 1 | ||
end | ||
|
||
odd_count = 0 | ||
# check the hash for characters that appear an odd number of times | ||
# the varialbe - key - is not being used thus _key | ||
letters.each do |_key, value| | ||
# add to odd character count | ||
odd_count += 1 if value.odd? | ||
end | ||
odd_count <= 1 | ||
|
||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,29 @@ | ||||||||||||||
|
||||||||||||||
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. 👍 |
||||||||||||||
raise NotImplementedError, "permutations? not implemented" | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
def str_to_hash(string) | ||||||||||||||
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. Mostly a style comment: I don't like putting a method in a method in Ruby. In JavaScript it's natural, not so much here. |
||||||||||||||
letters = {} | ||||||||||||||
i = 0 | ||||||||||||||
while string.length > i | ||||||||||||||
# check if string[index] is already a key in letters hash | ||||||||||||||
if letters.key?(string[i]) | ||||||||||||||
# if already a key, increment value at said key by 1 | ||||||||||||||
letters[string[i]] += 1 | ||||||||||||||
else | ||||||||||||||
# if NOT already a key, set the string[index] as a key in letters hash with a value of 1 | ||||||||||||||
letters[string[i]] = 1 | ||||||||||||||
end | ||||||||||||||
# increment loop | ||||||||||||||
i += 1 | ||||||||||||||
end | ||||||||||||||
letters | ||||||||||||||
end | ||||||||||||||
# call helper method on input strings | ||||||||||||||
string1_hash = str_to_hash(string1) | ||||||||||||||
string2_hash = str_to_hash(string2) | ||||||||||||||
# compare hashes | ||||||||||||||
if string1_hash != string2_hash | ||||||||||||||
false | ||||||||||||||
else | ||||||||||||||
true | ||||||||||||||
end | ||||||||||||||
Comment on lines
+24
to
+28
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.
Suggested change
|
||||||||||||||
end |
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.
👍