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
104 changes: 83 additions & 21 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,118 @@

# 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)
# Space complexity: O(1)
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
i
# raise NotImplementedError
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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.

👍

raise NotImplementedError
i = 0
until array[i].nil?
print array[i]
i += 1
end
# raise NotImplementedError #.new('array')
end

# For an unsorted array, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def search(array, length, value_to_find)
Comment on lines +34 to 36
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 i == length
if array[i] == value_to_find
return true
end
i += 1
end
false# raise NotImplementedError
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)
# Space complexity: O(1)
def find_largest(array, length)
Comment on lines +49 to 51
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
max = array[0]
i = 1
until i == length
if max < array[i]
max = array[i]
end
i += 1
end
max
# raise NotImplementedError
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)
# Space complexity: O(1)
def find_smallest(array, length)
Comment on lines +66 to 68
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
min = array[0]
i = 1
while i < length
if min > array[i]
min = array[i]
end
i += 1
end
min
# raise NotImplementedError
end

# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def reverse(array, length)
Comment on lines +82 to 84
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
j = length - 1
while i < j
temp = array[i]
array[i] = array[j]
array[j] = temp
i += 1
j -= 1
end
array
# raise NotImplementedError
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)
# Space complexity: O(1)
def binary_search(array, length, value_to_find)
Comment on lines +100 to 102
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 are not great variable names here.

raise NotImplementedError
mid = length / 2
i = 0
j = length - 1
while i < j
if array[mid] == value_to_find
return true
elsif array[mid] < value_to_find
i = mid
mid = i + j / 2
else
j = mid
mid = i + j / 2
end
i += 1
j -= 1
end
false
# raise NotImplementedError
end

# Helper method provided to sort the array in ascending order
Expand Down