From 9d326f16bf5d6067c3150c167ceb06c0cdc2bc68 Mon Sep 17 00:00:00 2001 From: Antonia Date: Sun, 17 May 2020 23:56:55 -0700 Subject: [PATCH 1/3] array intersection tests passing --- lib/array_intersection.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..93fb13c 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + return [] if list1 == nil || list2 == nil + + hash = {} + + list1.each do |element| + hash[element] = true + end + + result = [] + + list2.each do |number| + if hash.has_key?(number) + result << number + end + end + result + end \ No newline at end of file From 822ef071c7543d0f9259f5bd4340a806cfb90d94 Mon Sep 17 00:00:00 2001 From: Antonia Date: Mon, 18 May 2020 00:41:31 -0700 Subject: [PATCH 2/3] palindrome permutation tests passing --- lib/palindrome_permutation.rb | 23 ++++++++++++++++++++++- test/palindrome_permutation_test.rb | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..ff5b3dd 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" + return true if string.empty? + + hash = {} + + string.split("").each do |letter| + if hash[letter].nil? + hash[letter] = 1 + else + hash[letter] += 1 + end + end + + odd = 0 + + hash.values.each do |v| + odd += 1 if v.odd? + end + if odd <= 1 + return true + else + return false + end end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 6e43777..246ccfd 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 From bbee98517aaee63b9a752c5b11a27b489339b194 Mon Sep 17 00:00:00 2001 From: Antonia Date: Wed, 20 May 2020 16:45:23 -0700 Subject: [PATCH 3/3] permutations tests passing --- lib/permutations.rb | 26 ++++++++++++++++++++++++-- test/permutations_test.rb | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..5af0a27 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,26 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return true if string1 == string2 + return false if string1.length != string2.length + + hash = {} + + string1.split('').each do |letter| + if hash[letter] + hash[letter] += 1 + else + hash[letter] = 1 + end + end + + string2.split('').each do |letter| + return false if !hash.key?(letter) + if hash[letter] > 0 + hash[letter] -= 1 + else + return false + end + end + + return true + end \ No newline at end of file diff --git a/test/permutations_test.rb b/test/permutations_test.rb index eb1ce8c..792d9ef 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