diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..3f9faf6 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,13 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + hash = {} + array = [] + list1.each do |num| + hash[num] = true + list2.each do |num2| + if hash[num2] + array << num2 + end + end + end + return array.uniq +end diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..22b154b 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,18 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + hash = {} + count = 0 + string_array = string.split("") + string_array.each do |char| + hash[char] = 0 + end + string_array.each do |char| + if hash[char] + hash[char] += 1 + end + if hash[char] % 2 == 0 + count += 1 + end + end + return count == string_array.length/2 +end diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..cb833de 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,21 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + hash = {} + if string1.length != string2.length + return false + end + count = 0 + string1 = string1.split("") + string2 = string2.split("") + hash = {} + string1.each do |char| + hash[char] = true + end + string2.each do |char2| + if !hash[char2] + return false + end + end + return true +end + 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