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
99 changes: 92 additions & 7 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,141 @@
# The restricted_array is terminated by 'nil' i.e. array[length] = nil
# Time complexity: ?
# Space complexity: ?
#
def length(array)
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
#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: ?
#
def print_array(array)
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
# raise NotImplementedError
i = 0
while array[i] != nil
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: ?
def search(array, length, value_to_find)
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
# raise NotImplementedError
i = 0
length.times do
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: ?
def find_largest(array, length)
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
# raise NotImplementedError
i = 0
max_int = 0
return nil if length <= 0

length.times do
if array[i] > max_int
max_int = array[i]
end
i += 1
end
return max_int
end

# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
def find_smallest(array, length)
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
# raise NotImplementedError
i = 0
min_int = 221
return nil if length <= 0

length.times do
if array[i] < min_int
min_int = array[i]
end
i += 1
end
return min_int
end


# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
def reverse(array, length)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This works, but can you think of a way to do it without the hold array?

raise NotImplementedError
# raise NotImplementedError
if length <= 1
return array
end

i = 0
j = length - 1

hold_arr = RestrictedArray.new(length)

while i < length
hold_arr[i] = array[j]
i += 1
j -= 1
end

i = 0

while i < length
array[i] = hold_arr[i]
i += 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: ?
def binary_search(array, length, value_to_find)
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
# raise NotImplementedError
return false if length <= 0

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 array[low] == value_to_find
end


# Helper method provided to sort the array in ascending order
# Implements selection sort
# Time complexity = O(n^2), where n is the number of elements in the array.
Expand Down
1 change: 1 addition & 0 deletions test/using_restricted_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../lib/restricted_array'
require_relative '../lib/using_restricted_array'


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