diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..8a70536 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,25 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + lookup_hash = {} + + if list1.length < list2.length + list1.each do |num| + lookup_hash[num] = true + end + + intersection_array = list2.map do |num| + num if lookup_hash[num] + end + + return intersection_array.compact + else + list2.each do |num| + lookup_hash[num] = true + end + + intersection_array = list1.map do |num| + num if lookup_hash[num] + end + + return intersection_array.compact + end end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..ecfd0de 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,25 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + lookup_hash = {} + string.each_char do |letter| + lookup_hash[letter] = 0 + end + + string.each_char do |letter| + if lookup_hash[letter] + lookup_hash[letter] += 1 + end + end + + number_of_odd = 0 + lookup_hash.each_value do |letter| + number_of_odd += 1 if letter.odd? + end + + if number_of_odd > 1 + return false + else + return true + end + end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..8cd63ec 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,21 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return false if string1.length != string2.length + + lookup_hash = {} + string1.each_char do |letter| + lookup_hash[letter] = 0 + end + + string2.each_char do |letter| + if lookup_hash[letter] + lookup_hash[letter] = true + end + end + + puts lookup_hash + if lookup_hash.value?(0) + return false + else + return true + end end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..984b333 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 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