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

Leaves - Alice #26

Open
wants to merge 1 commit 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
122 changes: 101 additions & 21 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 42 additions & 40 deletions test/using_restricted_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,61 +33,61 @@
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)
size.times do |i|
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -150,40 +152,40 @@
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)
size.times do |i|
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)
size.times do |i|
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)
size.times do |i|
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