From d024d77c14089033e9b4b44557dafffdf03139ac Mon Sep 17 00:00:00 2001 From: Haben Date: Mon, 23 Mar 2020 23:11:31 -0700 Subject: [PATCH 1/4] added a method to find intersection --- lib/array_intersection.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..35d35bc 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ -def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file +#Design and implement a method that takes two integer arrays with unique +#values and returns their intersection in a new array. +def intersection(array1, array2) + intersection = [] + new_hash = {} + + array1.each do |number1| + new_hash[number1] = 1 + end + + array2.each do |number2| + if new_hash[number2] == 1 + intersection << number2 + end + end + return intersection + +end + From 65c43120737c7241af365e7a02f99b18716d67be Mon Sep 17 00:00:00 2001 From: Haben Date: Mon, 23 Mar 2020 23:12:05 -0700 Subject: [PATCH 2/4] added method to find permutations --- lib/permutations.rb | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..6f103b2 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,36 @@ + def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + first_hash = {} + second_hash = {} + + string1.chars.each do |letter| + first_hash[letter] = 0 + end + + string1.chars.each do |letter| + first_hash[letter] += 1 + end + + string2.chars.each do |letter| + second_hash[letter] = 0 + end + + string2.chars.each do |letter| + second_hash[letter] += 1 + end + + first_hash.each do |key, value| + if second_hash.has_key?(key) + if second_hash[key] == value + + else + return false + end + else + return false + end + end + return true +end + From 39a62f6574a6643633efe0475ccd754440072302 Mon Sep 17 00:00:00 2001 From: Haben Date: Wed, 25 Mar 2020 17:24:48 -0700 Subject: [PATCH 3/4] palindrome_permutaion? method added --- lib/array_intersection.rb | 1 + lib/palindrome_permutation.rb | 26 +++++++++++++++++++++++--- lib/permutations.rb | 3 ++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 35d35bc..7cd0a3f 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,5 +1,6 @@ #Design and implement a method that takes two integer arrays with unique #values and returns their intersection in a new array. + def intersection(array1, array2) intersection = [] new_hash = {} diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..e615dcd 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 +# Write a method which takes a string as an argument and returns +# true if the letters could be re-arranged into a palindrome. + + def palindrome_permutation?(string) + pal_per = {} + + string.chars.each do |letter| + if pal_per[letter] == nil + pal_per[letter] = 1 + else + pal_per[letter] += 1 + end + end + + odd_count = 0 + pal_per.each do |letter, count| + if count % 2 == 1 + odd_count += 1 + end + end + return odd_count <= 1 + end + \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 6f103b2..ae8e00b 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,5 @@ - +# Write a method which will take two strings as arguments +# and returns true if one string is a permutation of the other. def permutations?(string1, string2) first_hash = {} From da103f2823bcf9eb1e39c8a10c488969bd972193 Mon Sep 17 00:00:00 2001 From: Haben Date: Sun, 29 Mar 2020 23:21:28 -0700 Subject: [PATCH 4/4] all tests passing --- lib/array_intersection.rb | 1 - test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 7cd0a3f..8d0cc11 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -15,6 +15,5 @@ def intersection(array1, array2) end end return intersection - 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