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: 21 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
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"
intersection = {}
short_list = []
long_list = []
results = []
if list1.length < list2.length
short_list = list1
long_list = list2
else
short_list = list2
long_list = list1
end
short_list.each do |num|
intersection[num] = true
end

long_list.each do |num|
if intersection[num]
results.push(num)
end
end
return results
end
23 changes: 21 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
end
array_string = string.chars
palindrome_chars = Hash.new(0)
array_string.each do |ch|
palindrome_chars[ch] += 1
end
char_counts = palindrome_chars.values
puts char_counts
if string.length % 2 != 0
middle_char = char_counts.find_index {|i|
i % 2 == 1
}
if middle_char == nil
return false
end
char_counts.delete_at(middle_char)

Choose a reason for hiding this comment

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

This works, but if I can suggest, just count the number of letters which appear an odd number of times and it should be less than or equal to 1.

end
return char_counts.all? { |ch_num|
ch_num % 2 == 0 || ch_num == 0
}
end
palindrome_permutation?('carrace')
16 changes: 15 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@

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"
if string1.length != string2.length
return false
end
common_chars = Hash.new(0)
string1.chars.each do |ch|
common_chars[ch] += 1
end
string2.chars.each do |ch|
if common_chars[ch] == 0
return false
else
common_chars[ch] -= 1
end
end
return true
end
1 change: 1 addition & 0 deletions test/array_intersection_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "test_helper"


describe "Array Intersection" do
it "returns [4] for [2, 3, 4], and [4, 5, 6]" do
expect(intersection([2, 3, 4], [4, 5, 6])).must_equal [4]
Expand Down
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