From f10670a6a1cd6a871ad3cdd6f137a0141407c1cf Mon Sep 17 00:00:00 2001 From: Alice Date: Sun, 8 Sep 2019 21:07:38 -0700 Subject: [PATCH] using_restricted_array --- lib/using_restricted_array.rb | 122 +++++++++++++++++++++++----- test/using_restricted_array_test.rb | 82 ++++++++++--------- 2 files changed, 143 insertions(+), 61 deletions(-) diff --git a/lib/using_restricted_array.rb b/lib/using_restricted_array.rb index 90fe0d1..33b15db 100644 --- a/lib/using_restricted_array.rb +++ b/lib/using_restricted_array.rb @@ -6,56 +6,136 @@ # Calculates the length of the restricted array. All values are integers. # The restricted_array is terminated by 'nil' i.e. array[length] = nil -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because the only memory being altered is that one length integer, independent of the size of the data set on which it operates. def length(array) - raise NotImplementedError + length = 0 + + while array[length] != nil + length+=1 + end + + return length end # Prints each integer values in the array -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because there is just one variable (the counter; i). def print_array(array) - raise NotImplementedError + array_length = length(array) + i = 1 + + array_length.times do + puts array[i] + i+=1 + end end # For an unsorted array, searches for 'value_to_find'. # Returns true if found, false otherwise. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because there is no additional memory being taken, it just returns true/false. def search(array, length, value_to_find) - raise NotImplementedError + i = 0 + + while i < length + if array[i] == value_to_find + return true + else + i+=1 + end + end + + return false end # Finds and returns the largest integer value the array # Assumes that the array is not sorted. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because the only memory being modified is the "max_value" and "i", no matter the size of the array. def find_largest(array, length) - raise NotImplementedError + if length == 0 + return nil + end + + max_value = array[0] + i = 1 + + while i < length + if array[i] > max_value + max_value = array[i] + else + i+=1 + end + end + + return max_value end # Finds and returns the smallest integer value in the array # Assumes that the array is not sorted. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because the only memory being modified is the "min_value" and "i", no matter the size of the array. def find_smallest(array, length) - raise NotImplementedError + if length == 0 + return nil + end + + min_value = array[0] + i = 1 + + while i < length + if array[i] < min_value + min_value = array[i] + else + i+=1 + end + end + + return min_value end # Reverses the values in the integer array in place -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), linear, because the amount of times the while loop iterates is directly proportionate to the length of the array. +# Space complexity: O(1), constant, because the amount of variables being stored does not change regardless of the size of the array or data being passed through. def reverse(array, length) - raise NotImplementedError + i = 0 + j = length - 1 + + while i < j + temp = array[i] + array[i] = array[j] + array[j] = temp + + i+=1 + j-=1 + end end # For an array sorted in ascending order, searches for 'value_to_find'. # Returns true if found, false otherwise. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(log n), logarithmic, because increasing the size of the data set has a relatively small effect on the amount of times the loop has to iterate. +# Space complexity: O(1), constant, because the amount of variables being stored does not change regardless of the size of the array or data being passed through. def binary_search(array, length, value_to_find) - raise NotImplementedError + if length == 0 + return false + end + + low = 0 + high = length - 1 + + while low <= high + mid = (low+high)/2 + if array[mid] == value_to_find + return true + elsif array[mid] > value_to_find + high = mid - 1 + elsif array[mid] < value_to_find + low = mid + 1 + end + end + + return false end # Helper method provided to sort the array in ascending order diff --git a/test/using_restricted_array_test.rb b/test/using_restricted_array_test.rb index 5bf65c7..3d5d697 100644 --- a/test/using_restricted_array_test.rb +++ b/test/using_restricted_array_test.rb @@ -3,26 +3,28 @@ require_relative '../lib/restricted_array' require_relative '../lib/using_restricted_array' +Minitest::Reporters.use! + describe "restricted array" do it "length method" do size = 5 my_integer_array = RestrictedArray.new(size) - + my_integer_array_length = length(my_integer_array) - + my_integer_array_length.must_equal size end - + it "linear search method - value exists in the middle, in the array" do size = 9 my_integer_array = RestrictedArray.new(size) value_to_find = 220 middle_index = size / 2 my_integer_array[middle_index] = value_to_find - + search(my_integer_array, size, value_to_find).must_equal true end - + it "linear search method - value exists at the last index in the array" do size = 7 my_integer_array = RestrictedArray.new(size) @@ -31,10 +33,10 @@ end value_to_find = 220 my_integer_array[size - 1] = value_to_find - + search(my_integer_array, size, value_to_find).must_equal true end - + it "linear search method - value does not exist in array" do size = 4 my_integer_array = RestrictedArray.new(size) @@ -42,50 +44,50 @@ my_integer_array[i] = i end value_to_find = 220 - + search(my_integer_array, size, value_to_find).must_equal false end - + it "find largest in unsorted array" do size = 17 my_integer_array = RestrictedArray.new(size) - + largest = find_largest(my_integer_array, size) - + sort(my_integer_array, size) largest.must_equal my_integer_array[size - 1] end - + it "find largest in sorted array" do size = 14 my_integer_array = RestrictedArray.new(size) sort(my_integer_array, size) - + largest = find_largest(my_integer_array, size) - + largest.must_equal my_integer_array[size - 1] end - + it "find smallest in unsorted array" do size = 12 my_integer_array = RestrictedArray.new(size) - + smallest = find_smallest(my_integer_array, size) - + sort(my_integer_array, size) smallest.must_equal my_integer_array[0] end - + it "find smallest in sorted array" do size = 11 my_integer_array = RestrictedArray.new(size) sort(my_integer_array, size) - + smallest = find_smallest(my_integer_array, size) - + smallest.must_equal my_integer_array[0] end - + it "reverse array - odd count" do size = 9 my_integer_array = RestrictedArray.new(size) @@ -94,15 +96,15 @@ test_array[i] = my_integer_array[i] end test_array.reverse! - + reverse(my_integer_array, size) - + length(my_integer_array).must_equal size size.times do |i| my_integer_array[i].must_equal test_array[i] end end - + it "reverse array - even count" do size = 8 my_integer_array = RestrictedArray.new(size) @@ -111,37 +113,37 @@ test_array[i] = my_integer_array[i] end test_array.reverse! - + reverse(my_integer_array, size) - + length(my_integer_array).must_equal size size.times do |i| my_integer_array[i].must_equal test_array[i] end end - + it "find largest in sorted, reversed array" do size = 14 my_integer_array = RestrictedArray.new(size) sort(my_integer_array, size) reverse(my_integer_array, size) - + largest = find_largest(my_integer_array, size) - + largest.must_equal my_integer_array[0] end - + it "find smallest in sorted, reversed array" do size = 11 my_integer_array = RestrictedArray.new(size) sort(my_integer_array, size) reverse(my_integer_array, size) - + smallest = find_smallest(my_integer_array, size) - + smallest.must_equal my_integer_array[size - 1] end - + it "linear search method on sorted, reversed array - value exists in the array" do size = 13 my_integer_array = RestrictedArray.new(size) @@ -150,10 +152,10 @@ end value_to_find = 0 reverse(my_integer_array, size) - + search(my_integer_array, size, value_to_find).must_equal true end - + it "binary search method - value exists at the last index in the array" do size = 13 my_integer_array = RestrictedArray.new(size) @@ -161,10 +163,10 @@ my_integer_array[i] = i * 10 end value_to_find = (size - 1) * 10 - + binary_search(my_integer_array, size, value_to_find).must_equal true end - + it "binary search method - value exists at the middle index in the array" do size = 15 my_integer_array = RestrictedArray.new(size) @@ -172,10 +174,10 @@ my_integer_array[i] = i * 10 end value_to_find = (size/2) * 10 - + binary_search(my_integer_array, size, value_to_find).must_equal true end - + it "binary search method - value does not exist in the array" do size = 15 my_integer_array = RestrictedArray.new(size) @@ -183,7 +185,7 @@ my_integer_array[i] = i * 10 end value_to_find = size * 10 - + binary_search(my_integer_array, size, value_to_find).must_equal false end end