diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..7144eb7 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ + +# Design and implement a method that takes two integer arrays +# with unique values and returns their intersection in a new array. +# Note: Do not use the & operator. + def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + hash = {} + result = [] + list1.each do |num| + hash[num] = true + end + + list2.each do |i| + if hash[i] == true + result << i + end + end + return result end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..f0e7c84 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,32 @@ +# Write a method which will take two strings as arguments +# and returns true if one string is a permutation of the other. def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + hash = {} + count = 0 + + if string.empty? + return true + end + + string.each_char do |i| + if hash[i] == nil + hash[i] = 1 + else + hash[i] += 1 + end + end + + string_array = hash.values + string_array.each do |num| + if num.odd? == true + count += 1 + if count > 1 + return false + end + end + end + return true + + end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..135af28 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,25 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + + hash = {} + if string1.length != string2.length + return false + end + + string1.each_char do |letter| + if hash[letter] + hash[letter] += 1 + else + hash[letter] = 1 + end + end + + string2.each_char do |letter| + if hash[letter] && hash[letter] != 0 + hash[letter] -= 1 + else + return false + end + end + return true end \ No newline at end of file