From f40f45f28b5ea1b8bfd2875aaf360d9f93b10e3d Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Sun, 29 Mar 2020 22:06:45 -0700 Subject: [PATCH 1/5] passed all test for array intersection --- lib/array_intersection.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..e29de44 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,26 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + # add to hash if not already in the hash + # setting up lookup hash + lookup_hash = {} + if list1.length < list2.length + list1.each do |num| + lookup_hash[num] = true + end + + intersection_array = list2.map do |num| + num if lookup_hash[num] + end + + return intersection_array.compact + else + list2.each do |num| + lookup_hash[num] = true + end + + intersection_array = list1.map do |num| + num if lookup_hash[num] + end + + return intersection_array.compact + end end \ No newline at end of file From 79b24606e6143ce7e24e21b48155727788fd0004 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Sun, 29 Mar 2020 22:43:28 -0700 Subject: [PATCH 2/5] passed all tests for permutations --- lib/permutations.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..fc56532 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,23 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + + return false if string1.length != string2.length + + lookup_hash = {} + string1.each_char do |letter| + lookup_hash[letter] = 0 + end + + string2.each_char do |letter| + if lookup_hash[letter] + lookup_hash[letter] = true + end + end + + puts lookup_hash + if lookup_hash.value?(0) + return false + else + return true + end + end \ No newline at end of file From 557cd15d56f56ca9ada634c3e66ec5b24f1bf20a Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Sun, 29 Mar 2020 22:56:17 -0700 Subject: [PATCH 3/5] unskipped tests --- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 50003edc4c2cb5ba94a1620e614db16602117f98 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Sun, 29 Mar 2020 23:20:27 -0700 Subject: [PATCH 4/5] passed all tests --- lib/palindrome_permutation.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..ecfd0de 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,25 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + lookup_hash = {} + string.each_char do |letter| + lookup_hash[letter] = 0 + end + + string.each_char do |letter| + if lookup_hash[letter] + lookup_hash[letter] += 1 + end + end + + number_of_odd = 0 + lookup_hash.each_value do |letter| + number_of_odd += 1 if letter.odd? + end + + if number_of_odd > 1 + return false + else + return true + end + end \ No newline at end of file From 3ffc8c977c7005102a972be901017f732156e092 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Sun, 29 Mar 2020 23:20:40 -0700 Subject: [PATCH 5/5] cleaned up code --- lib/array_intersection.rb | 3 +-- lib/permutations.rb | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index e29de44..8a70536 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,7 +1,6 @@ def intersection(list1, list2) - # add to hash if not already in the hash - # setting up lookup hash lookup_hash = {} + if list1.length < list2.length list1.each do |num| lookup_hash[num] = true diff --git a/lib/permutations.rb b/lib/permutations.rb index fc56532..8cd63ec 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,5 +1,4 @@ def permutations?(string1, string2) - return false if string1.length != string2.length lookup_hash = {} @@ -19,5 +18,4 @@ def permutations?(string1, string2) else return true end - end \ No newline at end of file