Skip to content
Open
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
76 changes: 68 additions & 8 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,113 @@
# 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
i = 0
while array[i] != nil
i += 1
end
return i
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
print 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
array[i] != nil ? true : false
while i < length
return true if array[i] == value_to_find
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: ?
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
return nil if length == 0
i = 0
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: ?
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
return nil if length == 0
i = 0
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: ?
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.

👍 , although i and j aren't great variable names here.

raise NotImplementedError
# raise NotImplementedError
return array if length == 0
i = 0
j = (length - 1)
# (length / 2).times do
while i < j
last = array[j]
array[j] = array[i]
array[i] = last
i += 1
j -= 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
return true if value_to_find == array[mid]
high = mid - 1 if array[mid] > value_to_find
low = mid + 1 if array[mid] < value_to_find
end
return true if array[low] == value_to_find
return false
end

# Helper method provided to sort the array in ascending order
Expand Down Expand Up @@ -89,4 +149,4 @@ def sort(array, length)
end
end
end
## --- END OF METHODS ---
## --- END OF METHODS ---