From c5ff98ff2483afebd0cf0c9f66aa35d4e9840bfd Mon Sep 17 00:00:00 2001 From: Pushpa Agr Date: Tue, 21 Aug 2018 23:16:34 -0700 Subject: [PATCH 1/2] code passess all test, round 1 --- lib/array_equals.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/array_equals.rb b/lib/array_equals.rb index 58e8369..ed753d5 100644 --- a/lib/array_equals.rb +++ b/lib/array_equals.rb @@ -1,5 +1,61 @@ +require 'pry' # 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 + + + if array1 == nil && array2 == nil + return true + elsif array1 == nil && array2 != nil + return false + elsif array1 != nil && array2 == nil + return false + end + + + array1_length = 0 + array1.each do |element| + array1_length = array1_length + 1 + end + + + array2_length = 0 + array2.each do |element| + array2_length = array2_length + 1 + end + + index1 = 0 + array1_length.times do + if (array1_length == array2_length) && (array1[index1] == array2[index1]) && (array2[index1] == array1[index1]) + index1 = index1 + 1 + end + end + + index2 = 0 + array2_length.times do + if (array2_length == array1_length) && (array2[index2] == array1[index2]) && (array2[index2] == array1[index2]) + index2 = index2 + 1 + end + end + + # puts index1 + # puts array1_length + # puts index2 + # puts array2_length + + + if (index1 == array2_length) && (index2 == array1_length) + return true + else + return false + end end +# +# # +# array1 = [10, 20, 30, 40, 50, 60] +# array2 = [] +# +# a = array_equals(array1, array2) +# puts a From 05b25da2f9e3d1713c54a22609ba0ebe87ec87cf Mon Sep 17 00:00:00 2001 From: Pushpa Agr Date: Tue, 21 Aug 2018 23:35:37 -0700 Subject: [PATCH 2/2] code condensed and removed repeative code, all tests passed --- lib/array_equals.rb | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/lib/array_equals.rb b/lib/array_equals.rb index ed753d5..f80c8d4 100644 --- a/lib/array_equals.rb +++ b/lib/array_equals.rb @@ -1,61 +1,39 @@ -require 'pry' # 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) - if array1 == nil && array2 == nil return true elsif array1 == nil && array2 != nil return false elsif array1 != nil && array2 == nil return false + elsif array1 == [] && array2 == [] + return true end - array1_length = 0 array1.each do |element| array1_length = array1_length + 1 end - array2_length = 0 array2.each do |element| array2_length = array2_length + 1 end - index1 = 0 - array1_length.times do - if (array1_length == array2_length) && (array1[index1] == array2[index1]) && (array2[index1] == array1[index1]) - index1 = index1 + 1 - end - end - - index2 = 0 - array2_length.times do - if (array2_length == array1_length) && (array2[index2] == array1[index2]) && (array2[index2] == array1[index2]) - index2 = index2 + 1 + if array1_length == array2_length + index1 = 0 + array1_length.times do + if (array1[index1] == array2[index1]) + index1 = index1 + 1 + else + return false + end end - end - - # puts index1 - # puts array1_length - # puts index2 - # puts array2_length - - - if (index1 == array2_length) && (index2 == array1_length) return true else return false end end -# -# # -# array1 = [10, 20, 30, 40, 50, 60] -# array2 = [] -# -# a = array_equals(array1, array2) -# puts a