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 @@

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"
# raise NotImplementedError, "Intersection not implemented"
intersec_result = []

hash = {}

list1.each do |number|
hash[number] = true
end

list2.each do |number|
if !hash[number].nil?
intersec_result << number
end
end

return intersec_result
end
30 changes: 28 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@

def count_chars_in_string (string)
hash = {}

string.each_char do |char|
if hash.key?(char)
hash[char] += 1
else
hash[char] = 1
end
end
return hash
end

# the number of characters with odd number of occurences.
# If this countcount happens to exceed 1 at any step,
# we conclude that a palindromic permutation isn't possible for the string.

def palindrome_permutation?(string)
Comment on lines +15 to 19

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

odd_ocurrences_count = 0
num_ocurrences_hash = count_chars_in_string(string)

num_ocurrences_hash.each do |key, value|
odd_ocurrences_count += value % 2
end

return odd_ocurrences_count < 2
end
27 changes: 25 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@


def count_chars_in_string (string)
hash = {}

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

return hash
end

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

👍 Well done

raise NotImplementedError, "permutations? not implemented"
end
# raise NotImplementedError, "permutations? not implemented"
return false if string1.length != string2.length
# Return a boolean value.

hash_string1_counting = count_chars_in_string(string1)
hash_string2_counting = count_chars_in_string(string2)

return hash_string1_counting == hash_string2_counting

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
6 changes: 5 additions & 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 All @@ -20,4 +20,8 @@
it "returns false if the number of a specific letter are different" do
expect(permutations?("pizza", "piza")).must_equal false
end

it "returns false if the letter counts differ" do
expect(permutations?("pizza", "ppiza")).must_equal false
end
end