Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +2 to 6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
30 changes: 29 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
23 changes: 22 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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