Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space - Diana #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
24 changes: 23 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
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"
lookup_hash = {}

if list1.length < list2.length
list1.each do |num|
lookup_hash[num] = true
end

intersection_array = list2.map do |num|
num if lookup_hash[num]
end

return intersection_array.compact
Comment on lines +9 to +13

Choose a reason for hiding this comment

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

Use map if you want to do an operation on each element in the array. In this case you want to get only some of the elements of the array.

Suggested change
intersection_array = list2.map do |num|
num if lookup_hash[num]
end
return intersection_array.compact
intersection_array = list2.select do |num|
lookup_hash[num]
end
return intersection_array

else
list2.each do |num|
lookup_hash[num] = true
end

intersection_array = list1.map do |num|
num if lookup_hash[num]
end

return intersection_array.compact
end
end
23 changes: 22 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
lookup_hash = {}
string.each_char do |letter|
lookup_hash[letter] = 0
end

string.each_char do |letter|
if lookup_hash[letter]
lookup_hash[letter] += 1
end
end

number_of_odd = 0
lookup_hash.each_value do |letter|
number_of_odd += 1 if letter.odd?
end

if number_of_odd > 1
return false
else
return true
end
Comment on lines +19 to +23

Choose a reason for hiding this comment

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

Suggested change
if number_of_odd > 1
return false
else
return true
end
return number_of_odd > 1


end
21 changes: 19 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

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 would fail for something like heelo and hello. Instead of making lookup_hash[letter] = true instead count the number of times each letter appears.

raise NotImplementedError, "permutations? not implemented"
return false if string1.length != string2.length

lookup_hash = {}
string1.each_char do |letter|
lookup_hash[letter] = 0
end

string2.each_char do |letter|
if lookup_hash[letter]
lookup_hash[letter] = true
end
end

puts lookup_hash
if lookup_hash.value?(0)
return false
else
return true
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
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