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
22 changes: 19 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
def intersection(list1, list2)
raise NotImplementedError, "Intersection not implemented"
end
#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)
Comment on lines +1 to +4

Choose a reason for hiding this comment

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

👍

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

26 changes: 23 additions & 3 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
end
# 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)
Comment on lines +2 to +5

Choose a reason for hiding this comment

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

👍

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

37 changes: 35 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

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

👍 This works!

Just a note, you're repeating things a few times here, maybe a helper method?

raise NotImplementedError, "permutations? not implemented"
end
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

2 changes: 1 addition & 1 deletion test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/permutations_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down