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
89 changes: 82 additions & 7 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,128 @@
# Time complexity: ?
# Space complexity: ?
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
while array[i] != nil
i += 1
end
return i
# raise NotImplementedError
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
def print_array(array)
Comment on lines 20 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
while array[i] != nil
puts array[i]
i += 1
end
# raise NotImplementedError
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)
Comment on lines 32 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
while i < length
if array[i] == value_to_find
return true
end
i += 1
end
return false
# raise NotImplementedError
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)
Comment on lines 48 to 52
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
if length < 1
return nil
end

i = 0
largest_value = array[0]
while i < length
if array[i] > largest_value
largest_value = array[i]
end
Comment on lines +60 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if array[i] > largest_value
largest_value = array[i]
end
if array[i] > largest_value
largest_value = array[i]
end

i += 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: ?
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
if length < 1
return nil
end
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
# raise NotImplementedError
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.

👍

raise NotImplementedError
if length < 1
return nil
end
i = 0
rev = length - 1
while i < rev
holder = array[i]
array[i] = array[rev]
array[rev] = holder
i += 1
rev -= 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: ?
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
if length < 1
return nil
end
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
if array[low] == value_to_find
return true
end
return false
# raise NotImplementedError
Comment on lines +129 to +133
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some boolean Zen.

Suggested change
if array[low] == value_to_find
return true
end
return false
# raise NotImplementedError
return array[low] == value_to_find

end

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