Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
20 changes: 18 additions & 2 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
def intersection(list1, list2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
21 changes: 19 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

def palindrome_permutation?(string)

Choose a reason for hiding this comment

The 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
31 changes: 28 additions & 3 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "permutations? not implemented"
end

def str_to_hash(string)

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if string1_hash != string2_hash
false
else
true
end
return string1_hash != string2_hash

end
7 changes: 4 additions & 3 deletions test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "palindrome_permutation?" do
describe "palindrome_permutation?" do
it "will work for hello" do
expect(palindrome_permutation?("hello")).must_equal false
end
Expand All @@ -19,5 +19,6 @@

it "will return false for raceca" do
expect(palindrome_permutation?("raceca")).must_equal false
end
end
end

end
2 changes: 1 addition & 1 deletion test/permutations_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "permutations?" do
describe "permutations?" do
it "returns true for empty string" do
expect(permutations?("", "")).must_equal true
end
Expand Down