From b28b01d4bb4ea7a707172c01ae87f16c20f6033e Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 29 Mar 2020 12:55:58 -0700 Subject: [PATCH 1/5] pass tests with array --- lib/array_intersection.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..4e8c928 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,14 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + new_array = [] + + list1.each do |num| + list2.each do |num2| + if num == num2 + new_array << num + break + end + end + end + + return new_array end \ No newline at end of file From d8760753002f3675923b310d0f095a64342acf1f Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 29 Mar 2020 12:59:52 -0700 Subject: [PATCH 2/5] pass tests with hash map --- lib/array_intersection.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 4e8c928..75777b1 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,14 +1,25 @@ def intersection(list1, list2) - new_array = [] + hash = {} + array = [] - list1.each do |num| - list2.each do |num2| - if num == num2 - new_array << num - break + list1.each do |i| + if hash[i/10] + hash[i/10] << i + else + hash[i/10] = [i] + end + end + + list2.each do |i| + if hash[i/10] + hash[i/10].each do |x| + if i == x + array << i + break + end end end end - return new_array + return array end \ No newline at end of file From 1a02ee3f16a7cee77d8fb6bb64b4b66c03b470c1 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 29 Mar 2020 13:09:43 -0700 Subject: [PATCH 3/5] pass all tests for permutations --- lib/permutations.rb | 22 ++++++++++++++++++++-- test/permutations_test.rb | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..e387489 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,22 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return false if string1.length != string2.length + + hash = {} + string1.length.times do |i| + if hash[string1[i]] + hash[string1[i]] += 1 + else + hash[string1[i]] = 1 + end + end + + string2.length.times do |i| + if hash[string2[i]] && hash[string2[i]] > 0 + hash[string2[i]] -= 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 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 From 79a4e4090c90bc6d718e7d1341dcda829557deea Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 29 Mar 2020 13:26:05 -0700 Subject: [PATCH 4/5] pass all tests for palindrome_permutation --- lib/palindrome_permutation.rb | 23 +++++++++++++++++++++-- test/palindrome_permutation_test.rb | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..93e245e 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,23 @@ - def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + hash = {} + + string.length.times do |i| + if hash[string[i]] + hash[string[i]] += 1 + else + hash[string[i]] = 1 + end + end + + + odd = 0 + hash.each do |k, v| + if v % 2 != 0 + if v == 1 + return false if (odd += 1) > 1 + end + 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..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 From bcdab631e12b332654d3829a9bdfb3b27876dc0e Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 29 Mar 2020 13:34:28 -0700 Subject: [PATCH 5/5] add time and space complexity --- lib/array_intersection.rb | 6 ++++-- lib/palindrome_permutation.rb | 3 +++ lib/permutations.rb | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 75777b1..f56ec6b 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,7 +1,8 @@ +# Time complexity: O(n^2) +# Space complexity: O(n) + def intersection(list1, list2) hash = {} - array = [] - list1.each do |i| if hash[i/10] hash[i/10] << i @@ -10,6 +11,7 @@ def intersection(list1, list2) end end + array = [] list2.each do |i| if hash[i/10] hash[i/10].each do |x| diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 93e245e..3f97db8 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,3 +1,6 @@ +# Time complexity: O(n) +# Space complexity: O(n) + def palindrome_permutation?(string) hash = {} diff --git a/lib/permutations.rb b/lib/permutations.rb index e387489..46847ca 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,3 +1,5 @@ +# Time complexity: O(n) +# Space complexity: O(n) def permutations?(string1, string2) return false if string1.length != string2.length