diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..cdcca64 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + 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 diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..39ed803 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,21 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + 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 diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..4259e29 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,29 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + + def str_to_hash(string) + 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 +end diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..9f1ef22 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -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 @@ -19,5 +19,6 @@ it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false - end -end \ No newline at end of file + end + +end diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..5f6ae77 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -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