Skip to content
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
100 changes: 79 additions & 21 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,114 @@

# 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) because I'm performing a linear search until the loop reaches nil in the array.
# Space complexity: O(1), because I'm only looking for 1 value, it doesn't matter what size the array is.
def length(array)
Comment on lines +9 to 11
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
i = 0
until array[i] == nil
i += 1
end
return i
end


# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1) because retrieving an element is constant, regardless of the size.
# Space complexity: O(1) because size of the array doesn't matter? Still retrieving one element at a time.
def print_array(array)
Comment on lines +21 to 23
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , however the time complexity is O(n) because you print out n elements and it increases as the size of the array increases proportionately.

raise NotImplementedError
i = 0
until array[i] == nil
i += 1
puts i
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) for an unsorted array, because of its linear search to find that value.
# Space complexity: O(1), because I'm only looking for 1 value.
def search(array, length, value_to_find)
Comment on lines +33 to 35
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
i = 0
while i < length
if value_to_find == array[i]
return true
end
i += 1
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) because it involves a linear search for an unsorted array. O(1), if it's sorted because the search will end if it's the first element (largest_value = array[0]).
# Space complexity: O(1), because I'm only looking for 1 value.
def find_largest(array, length)
Comment on lines +48 to 50
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
i = 1
largest_value = array[0]

while i < length
if array[i] > largest_value
largest_value = array[i]
end
i += 1
end
return largest_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) because it involves a linear search for an unsorted array. O(1), if it's sorted because the search will end if it's the first element (smallest_val = array[0]).
# Space complexity: O(1), because I'm only looking for 1 value.
def find_smallest(array, length)
Comment on lines +65 to 67
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
i = 1
smallest_value = array[0]
while i < length
if array[i] < smallest_value
smallest_value = array[i]
end
i += 1
end
return smallest_value
end

# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1), because I'm retrieving one element at a time?
# Space complexity: O(1)
def reverse(array, length)
Comment on lines +80 to 82
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , however the time complexity is O(n) because you loop n/2 times.

raise NotImplementedError
i = 0
last_val = length - 1

while i < last_val
temp = array[i]
array[i] = array[last_val]
array[last_val] = temp
i += 1
last_val -= 1
end

return array
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), to narrow down my search by n/2 after an iteration, and then n/4 if there's another iteration that's needed to find the value.
# Space complexity: O(1), because I'm only looking for 1 value.
def binary_search(array, length, value_to_find)
Comment on lines +99 to 101
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
low_val = 0
high_val = length - 1

while low_val <= high_val
mid = (low_val + high_val) / 2

if array[mid] == value_to_find
return true
elsif array[mid] > value_to_find
high_val = mid - 1
elsif array[mid] < value_to_find
low_val = mid + 1
end
end
return false
end

# Helper method provided to sort the array in ascending order
Expand Down
4 changes: 4 additions & 0 deletions test/using_restricted_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require_relative '../lib/restricted_array'
require_relative '../lib/using_restricted_array'

Minitest::Reporters.use!
Minitest::Reporters::SpecReporter.new


describe "restricted array" do
it "length method" do
# Arrange
Expand Down