diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..c31647d Binary files /dev/null and b/.DS_Store differ diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..8052e43 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,18 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + hash = {} + intersect = [] + + list1.each do |item| + hash[item] = true + end + + list2.each do |item| + if hash[item] + intersect << item + end + end + + return intersect + + end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..940a7d8 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,24 @@ - def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + if string.empty? + return true + end + + hash = {} + array = string.chars + + array.each do |char| + if hash[char] + hash[char] += 1 + else + hash[char] = 1 + end + end + + odd = 0 + + hash.each do |key, value| + odd += 1 if value % 2 != 0 + end + + odd > 1 ? false : true +end diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..1ee927b 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,23 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + + return true if string1.empty? || string2.empty? + return false if string1.length != string2.length + + array1 = string1.chars + array2 = string2.chars + + hash = {} + + array1.each do |char| + hash[char] = true + end + + array2.each do |char| + if hash[char] == nil + return false + end + end + + return true end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..b966841 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,5 @@ 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