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
15 changes: 14 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
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"
num_hash = {}

list1.each do |num|
num_hash[num] = 0
end

intersection_array = []

list2.each do |num|
if num_hash.has_key?(num)
intersection_array << num
end
end
return intersection_array
end
29 changes: 28 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@

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"
return true if string == ""

char_count_hash = {}

string.each_char do |char|
if char_count_hash.has_key?(char)
char_count_hash[char] = char_count_hash[char] + 1
else
char_count_hash[char] = 1
end
end

if (string.length % 2 == 0) #string is even or not?
#remove all pairs for which the num of chars is even
char_count_hash.reject! { |key, val| (val % 2 == 0) }
#if the hash is empty, it passes.
return char_count_hash.empty?
else
#remove all pairs for which the num of chars is even
char_count_hash.reject! { |key, val| (val % 2 == 0) }
#make sure there's only one pair left
if char_count_hash.length == 1
#if value is exactly 1, it passes.
return char_count_hash.value?(1)
else
return false
end
end
end
28 changes: 27 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@

def permutations?(string1, string2)
raise NotImplementedError, "permutations? not implemented"
if string1 == "" && string2 == ""
return true
elsif string1.length != string2.length
return false
end

char_count_hash = {}

string1.each_char do |char|
if char_count_hash.has_key?(char)
char_count_hash[char] = char_count_hash[char] + 1
else
char_count_hash[char] = 0
end
end

string2.each_char do |char|
if char_count_hash.has_key?(char)
if char_count_hash[char] == 0
char_count_hash.delete(char)
else
char_count_hash[char] = char_count_hash[char] - 1
end
end
end

char_count_hash.empty? ? (return true) : (return false)

Choose a reason for hiding this comment

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

Suggested change
char_count_hash.empty? ? (return true) : (return false)
return char_count_hash.empty?

end
2 changes: 1 addition & 1 deletion test/array_intersection_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

describe "Array Intersection" do
xdescribe "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]
end
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
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