-
Notifications
You must be signed in to change notification settings - Fork 47
Ports_Myriam #26
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
base: master
Are you sure you want to change the base?
Ports_Myriam #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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) | ||
There was a problem hiding this comment.
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:That also eliminates your need for the
switchvariable.