Skip to content
Open
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
46 changes: 44 additions & 2 deletions lib/array_equals.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
# Determines if the two input arrays have the same count of elements
# and the same integer values in the same exact order
def array_equals(array1, array2)
raise NotImplementedError

def array_equals(array_one, array_two)
if array_one.length == array_two.length
i = 0

# solution 1 --> It exits the loop when the item evaluates to false
# if first item is false it doesn't loop through the whole array.

switch = 0
while switch == 0 && i < array_one.length
array_one[i] != array_two[i] ? switch = 1 : i += 1

Choose a reason for hiding this comment

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

[nit] This is absolutely a correct implementation of this loop, but it's a little surprising. Normally in imperative languages like Ruby, I expect the ternary operator (the ? : syntax) to be used when assigning variables, but you aren't assigning the result of this operator to anything. As someone who likes to operate on the principle of least surprise, I'd find it more obvious if I read:

if (array_one[i] != array_two[i])
   return false
end
i += 1

That also eliminates your need for the switch variable.

end
return switch == 0 ? true : false

# # solution 2 --> It loops through the whole array and then compares the number
# of assertions to the length.

# assertion = 0

Choose a reason for hiding this comment

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

Just as a note, it's better not to leave commented-out code in committed files. However, in this case, it looks like you wanted us to see the second solution you came up with. Since it's here, I'll say: solution 1 is the better solution because it has the ability to stop looping as soon as it finds an element. The opportunity for algorithms to take shortcuts like that are things you should be looking for, so I'm glad you chose to leave solution 1 and not solution 2 in the code. :)

# (array_one.length).times do
# assertion += 1 if array_one[i] == array_two[i]
# i += 1
# end
# return assertion == array_one.length ? true : false

else
return false
end
end

Choose a reason for hiding this comment

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

Good job! That's a very elegant solution!

first_array_size = rand(1..2)
second_array_size = rand(1..2)

first_array = []

Choose a reason for hiding this comment

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

It's great to see you testing and trying out your code! Ideally, you shouldn't leave that code in this file, though. Don't feel afraid of adding test cases to the spec files when it makes sense as you work, and also don't feel afraid of adding a .rb file next to this one where this sort of code can live (and where you could run it separately).

first_array_size.times do
item = rand(1..2)
first_array << item
end

second_array = []
second_array_size.times do
item = rand(1..2)
second_array << item
end

puts array_equals(first_array, second_array)