From fe098858c2112a5007bdfc2134a25751ef4e74fe Mon Sep 17 00:00:00 2001 From: quinqu Date: Thu, 26 Mar 2020 12:28:31 -0700 Subject: [PATCH 1/2] initial commit --- lib/array_intersection.rb | 14 ++++++++++++-- lib/palindrome_permutation.rb | 24 ++++++++++++++++++++++-- lib/permutations.rb | 21 +++++++++++++++++++-- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..3f9faf6 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,13 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + hash = {} + array = [] + list1.each do |num| + hash[num] = true + list2.each do |num2| + if hash[num2] + array << num2 + end + end + end + return array.uniq +end diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..230d31b 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 + hash = {} + count = 0 + string_array = string.split("") + string_array.each do |char| + hash[char] = 0 + end + string_array.each do |char| + if hash[char] + hash[char] += 1 + end + if hash[char] % 2 == 0 + count += 1 + end + end + + if string.length % 2 == 0 + return count == string_array.length/2 + elsif string.length % 2 > 0 + return count == (string_array.length-1)/2 + end + return false +end diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..cb833de 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,21 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + hash = {} + if string1.length != string2.length + return false + end + count = 0 + string1 = string1.split("") + string2 = string2.split("") + hash = {} + string1.each do |char| + hash[char] = true + end + string2.each do |char2| + if !hash[char2] + return false + end + end + return true +end + 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 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 24fc218df6a095702d5e77d92b96eedc2cdde869 Mon Sep 17 00:00:00 2001 From: quinqu Date: Sun, 29 Mar 2020 19:30:14 -0700 Subject: [PATCH 2/2] removed unnecessary code --- lib/palindrome_permutation.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 230d31b..22b154b 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -14,11 +14,5 @@ def palindrome_permutation?(string) count += 1 end end - - if string.length % 2 == 0 return count == string_array.length/2 - elsif string.length % 2 > 0 - return count == (string_array.length-1)/2 - end - return false end