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
19 changes: 18 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
def intersection(list1, list2)

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"

intersections = {}

list1.each do |value|
if !intersections[value]
intersections[value] = 1
elsif intersections[value]
intersections[value] += 1
end
end

list2.each do |value|
if intersections[value]
intersections[value] += 1
end
end

return (intersections.select { |key, value| value == 2}).keys
end
19 changes: 18 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
string_array = string.split("")
letter_instances = {}

string_array.each do |letter|
if !letter_instances[letter]
letter_instances[letter] = 1
else
letter_instances[letter] += 1
end
end

duplicate_letters = letter_instances.select { |key, value| (value % 2) == 0}

if duplicate_letters.values.sum == string_array.length || duplicate_letters.values.sum == string_array.length - 1
return true
else
return false
end
Comment on lines +16 to +20

Choose a reason for hiding this comment

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

You can simplify this and eliminate the if statement, since you're looking for a true/false answer.

Suggested change
if duplicate_letters.values.sum == string_array.length || duplicate_letters.values.sum == string_array.length - 1
return true
else
return false
end
return duplicate_letters.values.sum == string_array.length || duplicate_letters.values.sum == string_array.length - 1

end
24 changes: 23 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@

def permutations?(string1, string2)
raise NotImplementedError, "permutations? not implemented"
intersections = {}
string1_array = string1.split("")
string2_array = string2.split("")

string1_array.each do |value|
if !intersections[value]
intersections[value] = 1
elsif intersections[value]
intersections[value] += 1
end
end

string2_array.each do |value|
if intersections[value]
intersections[value] += 1
end
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

What if the value isn't in intersections? Why not return false, if it's not there.

I would also suggest subtracting instead of adding, and then you can check to see if all the values are 0.

end

if ((intersections.select { |key, value| (value % 2) == 0}).values.sum / 2) == string1_array.length
return true
else
return false
end
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
4 changes: 2 additions & 2 deletions 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 All @@ -9,7 +9,7 @@
expect(permutations?("hello", "ehllo")).must_equal true
end

it "returns true for 'heelo', 'ehllo'" do
it "returns false for 'heelo', 'ehllo'" do
expect(permutations?("heelo", "ehllo")).must_equal false
end
it "returns true for 'pasta', 'atsap'" do
Expand Down