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

# 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
n = 0

until array[n] == nil
n += 1
end

return n
#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 +23 to 25
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
printed_elements = ""
n = 0

until array[n] == nil
printed_elements += "#{array[n]} "
n += 1
end
#raise NotImplementedError
return printed_elements
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 +39 to 41
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
n = 0
value_search = false

until n == length
if array[n] == value_to_find
value_search = true
end
n += 1
end

return value_search

#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 +59 to 61
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
largest_value = 0
n = 0

until n == length
if array[n] == nil
return false
elsif array[n] > largest_value
largest_value = array[n]
end
n += 1
end

return largest_value
#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 +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.

👍

raise NotImplementedError
smallest_value = 221
n = 0

until n == length
if array[n] == nil
return false
elsif array[n] < smallest_value
smallest_value = array[n]
end

n+=1

end

return smallest_value

#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 +103 to 105
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)

if length <= 1
return array
end

while i < j
temp = array[i]
array[i] = array[j]
array[j] = temp

i += 1
j -= 1
end

return 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(n)
def binary_search(array, length, value_to_find)
Comment on lines +128 to 130
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 = 0
high = length - 1

if length == 0
return false
end

while low <= high
mid = (low + high) / 2
if array[mid] == value_to_find
return true
elsif array[mid] > value_to_find
high = mid - 1
else
low = mid + 1
end
end

if array[mid] != value_to_find
return false
end
Comment on lines +149 to +151
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a little boolean zen

Suggested change
if array[mid] != value_to_find
return false
end
return array[mid] == value_to_find

#raise NotImplementedError
end

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