From 8b193e9e2cd96b13f78bb53cf79a54bd45efe414 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 29 Mar 2020 15:00:34 -0700 Subject: [PATCH 1/5] intersection --- lib/array_intersection.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..4493048 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,18 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + hash = {} + intersect = [] + + list1.each do |item| + hash[item] = true + end + + list2.each do |item| + if hash[item] + compare << element + end + end + + return intersect + + end \ No newline at end of file From a87b15d12409efa437c15e07d394f7d39cae0bf8 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 29 Mar 2020 15:16:41 -0700 Subject: [PATCH 2/5] intersection passes test --- lib/array_intersection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 4493048..8052e43 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -8,7 +8,7 @@ def intersection(list1, list2) list2.each do |item| if hash[item] - compare << element + intersect << item end end From 2650d734d6856b4d568861df11839234725bce02 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 29 Mar 2020 15:20:33 -0700 Subject: [PATCH 3/5] permutations --- lib/permutations.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..1ee927b 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,23 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + + return true if string1.empty? || string2.empty? + return false if string1.length != string2.length + + array1 = string1.chars + array2 = string2.chars + + hash = {} + + array1.each do |char| + hash[char] = true + end + + array2.each do |char| + if hash[char] == nil + return false + end + end + + return true end \ No newline at end of file From 83b40f89f0f8260e93b93e2f0342f0815fff26d2 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 29 Mar 2020 15:40:30 -0700 Subject: [PATCH 4/5] unblocked tests --- lib/palindrome_permutation.rb | 26 +++++++++++++++++++++++--- test/permutations_test.rb | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..4c63593 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 + if string.empty? + return true + end + + hash = {} + array = string.chars + + array.each do |char| + hash[char] += 1 + end + + count = 0 + + values = hash.values + values.each do |num| + if num.odd? == true + count += 1 + end + return false if count > 1 + end + + return true +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 ad725ea67075c297ce66999c4bec375e71360104 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 29 Mar 2020 15:44:13 -0700 Subject: [PATCH 5/5] passed test --- .DS_Store | Bin 0 -> 6148 bytes lib/palindrome_permutation.rb | 18 +++++++++--------- test/palindrome_permutation_test.rb | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c31647d80013270d841ba23600bcc8ebaaca9308 GIT binary patch literal 6148 zcmeHKO>Wab6n@hfI!T4d0;#a`28l(4k{~DuA(=E;L;{2`f(4+~u3c)?^;EG#1R=;9 z?ttI~T!jm85Doy}{3L1|*swq-niswKo@c%{_WL5wc!-Gg#!-i;Nkkkj(P^OhjmdH8 zE4E>JSz+4_tAJJDe^WrdyIXWfF@6!9eSf22Dh|R_gkZ?!;V%5z zPi2URYLrq!VTJXz!rG(fAJBd3 z(?g(Kbt+HIepE6XDlmIa#q!Lmx=&}STkyh3eriW~f+UMaDmjT_P+mSyvPevN zVw^>}n#b1zoY#4MuX$xUz1h9fay#7{w`VPPy0g=6xwpEV*{sgDuXgSY_fOuwo4%iY z_>8Jy46CxTiw5WL1;L`iH~uI~MfMtfbN-xXah(+~|2E%R=OS%wRspNPUnwBh2Ny2U zGgxX=O9vWt1OPhdR)$!9EXW+!pl7huh!GgmsX(2|%n^g>bo9H1=NT+D>U3h}@WITT znK_{_b$86~DmpPwqiwAMR)J*&YWlJ%_y574&;QFw_R1nC(~vax1 1 + hash.each do |key, value| + odd += 1 if value % 2 != 0 end - return true + odd > 1 ? false : true end diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..b966841 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 @@ -19,5 +19,5 @@ it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false - end -end \ No newline at end of file + end +end