From 9a36326250e200bbf9b5da4c574f6c52fac8ae67 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 31 Mar 2020 20:45:54 -0700 Subject: [PATCH 1/5] Passed array_intersection tests --- lib/array_intersection.rb | 16 +++++++++++++++- lib/palindrome_permutation.rb | 2 +- lib/permutations.rb | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..67dd8ac 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,17 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + # Design and implement a method that takes two integer arrays with unique values and returns their intersection in a new array. + num_hash = {} + + list1.each do |num| + num_hash[num] = 0 + end + + intersection_array = [] + + list2.each do |num| + if num_hash.has_key?(num) + intersection_array << num + 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..63dea29 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,4 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + # Write a method which takes a string as an argument and returns true if the letters could be re-arranged into a palindrome. end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..fe657a0 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,11 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + # Write a method which will take two strings as arguments and returns true if one string is a permutation of the other. + + if string1 == "" && string2 == "" + return true + elsif string1.length != string2.length + return false + end + end \ No newline at end of file From aa57439e4fb6d254ecd4991267a8ec330b744c28 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 31 Mar 2020 21:06:44 -0700 Subject: [PATCH 2/5] permutations passes all tests --- lib/array_intersection.rb | 1 - lib/permutations.rb | 23 +++++++++++++++++++++-- test/array_intersection_test.rb | 2 +- test/permutations_test.rb | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 67dd8ac..8317d19 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,5 +1,4 @@ def intersection(list1, list2) - # Design and implement a method that takes two integer arrays with unique values and returns their intersection in a new array. num_hash = {} list1.each do |num| diff --git a/lib/permutations.rb b/lib/permutations.rb index fe657a0..375574e 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,11 +1,30 @@ def permutations?(string1, string2) - # Write a method which will take two strings as arguments and returns true if one string is a permutation of the other. - if string1 == "" && string2 == "" return true elsif string1.length != string2.length return false end + char_count_hash = {} + + string1.each_char do |char| + if char_count_hash.has_key?(char) + char_count_hash[char] = char_count_hash[char] + 1 + else + char_count_hash[char] = 0 + end + end + + string2.each_char do |char| + if char_count_hash.has_key?(char) + if char_count_hash[char] == 0 + char_count_hash.delete(char) + else + char_count_hash[char] = char_count_hash[char] - 1 + end + end + end + + char_count_hash.empty? ? (return true) : (return false) end \ No newline at end of file diff --git a/test/array_intersection_test.rb b/test/array_intersection_test.rb index 4830afe..fb117a0 100644 --- a/test/array_intersection_test.rb +++ b/test/array_intersection_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe "Array Intersection" do +xdescribe "Array Intersection" do it "returns [4] for [2, 3, 4], and [4, 5, 6]" do expect(intersection([2, 3, 4], [4, 5, 6])).must_equal [4] 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 From f375dcdd3fe8df3dcbff3e4ea7f4f0e16f60177b Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 31 Mar 2020 21:24:59 -0700 Subject: [PATCH 3/5] passing most of tests for palindrome permutation, working to pass all --- lib/palindrome_permutation.rb | 28 ++++++++++++++++++++++++++++ test/palindrome_permutation_test.rb | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 63dea29..efe9ef2 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,32 @@ def palindrome_permutation?(string) # Write a method which takes a string as an argument and returns true if the letters could be re-arranged into a palindrome. + + + return true if string == "" + + char_count_hash = {} + + string.each_char do |char| + if char_count_hash.has_key?(char) + char_count_hash[char] = char_count_hash[char] + 1 + else + char_count_hash[char] = 1 + end + end + + if (string.length % 2 == 0) #string is even or not? + #remove all pairs for which the num of chars is even + char_count_hash.reject! { |key, val| (val % 2 == 0) } + #if the hash is empty, it passes. + return char_count_hash.empty? + else + #remove all pairs for which the num of chars is even + char_count_hash.reject! { |key, val| (val % 2 == 0) } + #make sure there's only one pair left + if char_count_hash.length == 1 + #if value is exactly 1, it passes. + return char_count_hash.value?(1) + end + end 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 ce0a5508af45c7d4b2b4e36b7ed934484c41e3c8 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 31 Mar 2020 21:27:17 -0700 Subject: [PATCH 4/5] Fixed bug, pass all tests --- lib/palindrome_permutation.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index efe9ef2..6f49746 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -27,6 +27,8 @@ def palindrome_permutation?(string) if char_count_hash.length == 1 #if value is exactly 1, it passes. return char_count_hash.value?(1) + else + return false end end end \ No newline at end of file From 0456e792a0d31728355fd5ade49a4d6e98df785f Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 31 Mar 2020 21:29:36 -0700 Subject: [PATCH 5/5] Removed extra comments from top --- lib/palindrome_permutation.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 6f49746..42b8e72 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,8 +1,5 @@ def palindrome_permutation?(string) - # Write a method which takes a string as an argument and returns true if the letters could be re-arranged into a palindrome. - - return true if string == "" char_count_hash = {}