Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Water - Jessica #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
103 changes: 81 additions & 22 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,115 @@

# 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)
#[2,3,6,8,1,3]
def length(array)
Comment on lines +9 to 12
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
return i
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
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: ?
# Time complexity: O(n)
# Space complexity: O(1)
def search(array, length, value_to_find)
Comment on lines +33 to 35
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
end

# Finds and returns the largest integer value the array
# Finds and returns the largest 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_largest(array, length)
Comment on lines +48 to 50
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
largest_value = 0
until 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: ?
# Time complexity: O(n)
# Space complexity: O(1)
def find_smallest(array, length)
Comment on lines +64 to 66
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
smallest_value = array[0]
until 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: ?
# Time complexity: O(n)
# Space complexity: O(1)
def reverse(array, length)
Comment on lines +79 to 81
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 = length - 1
j = 0
temp_value = 0
until i < j
temp_value = array[i]
array[i] = array[j]
array[j] = temp_value
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: ?
# Time complexity: O(log n)
# Space complexity: O(1)
# [1,3,6,7,9,13,17,22], 8, 22
# [1,2]
def binary_search(array, length, value_to_find)
Comment on lines +97 to 101
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 = 0
max = length-1
until min == max
mid = (min + max)/2
if value_to_find > array[mid]
min = mid + 1
elsif value_to_find < array[mid]
max = mid - 1
else
return true
end
end
if array[min] == value_to_find
return true
end
return false
end

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