diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..8d0cc11 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 + 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 3b08381..ae8e00b 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,37 @@ +# 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) - 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 + 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