From 60c73200f46c027930b868f1bf6ffaa524ed7e85 Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 2 Apr 2020 00:51:28 -0700 Subject: [PATCH 1/5] final code --- lib/array_intersection.rb | 30 ++++++++++++++++++++++++++- lib/palindrome_permutation.rb | 25 +++++++++++++++++++++- lib/permutations.rb | 32 ++++++++++++++++++++++++++++- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..e6891ab 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,31 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + hash_table = {} + intersection_array = [] + if list1.length < list2.length + list1.each do |num| + hash_table[num] = true + end + + list2.each do |num| + intersection_array << num if hash_table[num] != nil + end + elsif list2.length < list1.length + list2.each do |num| + hash_table[num] = false + end + + list1.each do |num| + intersection_array << num if hash_table[num] != nil + end + else + list1.each do |num| + hash_table[num] = false + end + + list2.each do |num| + intersection_array << num if hash_table[num] != nil + end + end + + return intersection_array end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..8a2dc60 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,27 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + return true if string.length == 1 || string.length == 0 + + split_string = string.chars + hash_table = {} + + split_string.each do |char| + value = hash_table[char] + hash_table[char]? hash_table[char] = value +1 : hash_table[char] = 1 + end + + if string.length.even? + odd_count = 0 + hash_table.values.each do |value| + odd_count += 1 if value.odd? + end + odd_count == 0? true : false + + elsif string.length.odd? + odd_count = 0 + hash_table.values.each do |value| + odd_count += 1 if value.odd? + end + return odd_count == 1 + end end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..b821217 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,34 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return true if string1.empty? && string2.empty? + + split_string1 = string1.chars + split_string2 = string2.chars + + hash_table = {} + + split_string1.each do |char| + if hash_table[char] == nil + hash_table[char] = 1 + else + value = hash_table[char] + hash_table[char] = value + 1 + end + end + + split_string2.each do |char| + value = hash_table[char] + if hash_table[char] == nil + return false + else + hash_table[char] = value - 1 + end + end + + if hash_table.values.min < 0 || hash_table.values.max > 0 + return false + elsif hash_table.values.sum == 0 + 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 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 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 From 7b3640099218fbc151b288fd1188640be8961efc Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 2 Apr 2020 02:03:44 -0700 Subject: [PATCH 2/5] refactor permutation --- lib/permutations.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index b821217..cee0030 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -8,21 +8,13 @@ def permutations?(string1, string2) hash_table = {} split_string1.each do |char| - if hash_table[char] == nil - hash_table[char] = 1 - else - value = hash_table[char] - hash_table[char] = value + 1 - end + value = hash_table[char] + hash_table[char] ? hash_table[char] = value + 1 : hash_table[char] = 1 end split_string2.each do |char| value = hash_table[char] - if hash_table[char] == nil - return false - else - hash_table[char] = value - 1 - end + hash_table[char] ? hash_table[char] = value - 1 : false end if hash_table.values.min < 0 || hash_table.values.max > 0 From 6da396420fd5dc64ecae5cf913ff0b956a79701b Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 2 Apr 2020 02:13:50 -0700 Subject: [PATCH 3/5] refactor array intersectiom --- lib/array_intersection.rb | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index e6891ab..d646c03 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,31 +1,14 @@ def intersection(list1, list2) hash_table = {} intersection_array = [] - if list1.length < list2.length - list1.each do |num| - hash_table[num] = true - end - - list2.each do |num| - intersection_array << num if hash_table[num] != nil - end - elsif list2.length < list1.length - list2.each do |num| - hash_table[num] = false - end - - list1.each do |num| - intersection_array << num if hash_table[num] != nil - end - else - list1.each do |num| - hash_table[num] = false - end - - list2.each do |num| - intersection_array << num if hash_table[num] != nil - end + + list1.each do |num| + hash_table[num] = 1 end + list2.each do |num| + intersection_array << num if hash_table[num] + end + return intersection_array end \ No newline at end of file From 41aa100ca710b2075bd544dfa4d0e5d324219d86 Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 2 Apr 2020 02:17:35 -0700 Subject: [PATCH 4/5] add space and time complexity --- lib/array_intersection.rb | 3 +++ lib/palindrome_permutation.rb | 3 ++- lib/permutations.rb | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index d646c03..1e0ad4d 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,6 @@ +# time complexity: O(n + m) +# space complexity: O(n) + def intersection(list1, list2) hash_table = {} intersection_array = [] diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 8a2dc60..04734b5 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,5 @@ - +# time complexity: O(n) +# space complexity: O(n) def palindrome_permutation?(string) return true if string.length == 1 || string.length == 0 diff --git a/lib/permutations.rb b/lib/permutations.rb index cee0030..d155f7b 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,3 +1,5 @@ +# time complexity: O(n + m) +# space complexity: O(n) def permutations?(string1, string2) return true if string1.empty? && string2.empty? From 7e77ad6f8090d617b52756dc50db87d1d98d25cc Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 2 Apr 2020 02:34:04 -0700 Subject: [PATCH 5/5] drying up palindrome and cleaining up permutations --- lib/palindrome_permutation.rb | 17 ++++------------- lib/permutations.rb | 6 +----- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 04734b5..a384873 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -11,18 +11,9 @@ def palindrome_permutation?(string) hash_table[char]? hash_table[char] = value +1 : hash_table[char] = 1 end - if string.length.even? - odd_count = 0 - hash_table.values.each do |value| - odd_count += 1 if value.odd? - end - odd_count == 0? true : false - - elsif string.length.odd? - odd_count = 0 - hash_table.values.each do |value| - odd_count += 1 if value.odd? - end - return odd_count == 1 + odd_count = 0 + hash_table.values.each do |value| + odd_count += 1 if value.odd? end + odd_count <= 1 ? true : false end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index d155f7b..5dd0902 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -19,10 +19,6 @@ def permutations?(string1, string2) hash_table[char] ? hash_table[char] = value - 1 : false end - if hash_table.values.min < 0 || hash_table.values.max > 0 - return false - elsif hash_table.values.sum == 0 - return true - end + return hash_table.values.sum == 0 && hash_table.values.min == 0 ? true : false end \ No newline at end of file