-
Notifications
You must be signed in to change notification settings - Fork 53
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
Leah - Water #48
base: master
Are you sure you want to change the base?
Leah - Water #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -6,56 +6,123 @@ | |||
|
||||
# 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) | ||||
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(n) | ||||
def print_array(array) | ||||
Comment on lines
+21
to
23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 , this mostly makes a copy of the list instead, but you are correct in time/space complexity. |
||||
raise NotImplementedError | ||||
printed_list = "" | ||||
|
||||
(length(array)).times do |i| | ||||
if i + 1 == length(array) | ||||
printed_list << "#{array[i]}" | ||||
else | ||||
printed_list << "#{array[i]} " | ||||
end | ||||
i += 1 | ||||
end | ||||
|
||||
return printed_list | ||||
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
+42
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||
raise NotImplementedError | ||||
|
||||
length.times do |i| | ||||
if array[i] == value_to_find | ||||
return true | ||||
end | ||||
end | ||||
|
||||
return false | ||||
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
+58
to
60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||
raise NotImplementedError | ||||
largest_value = 0 | ||||
i = 0 | ||||
while i < length | ||||
largest_value = array[i] if array[i] > largest_value | ||||
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
+74
to
76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||
raise NotImplementedError | ||||
smallest_value = array[0] | ||||
i = 0 | ||||
# Why doesn't until i = length work here? | ||||
while i < length | ||||
smallest_value = array[i] if array[i] < smallest_value | ||||
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
+90
to
92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||
raise NotImplementedError | ||||
i = 0 | ||||
while i < (length/2) | ||||
pp i | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debugging statement not needed in the submission.
Suggested change
|
||||
temp_holder = array[i] | ||||
array[i] = array[length - (i + 1)] | ||||
array[length - (i + 1)] = temp_holder | ||||
i += 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) | ||||
def binary_search(array, length, value_to_find) | ||||
Comment on lines
+108
to
110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||
raise NotImplementedError | ||||
index_low = 0 | ||||
index_high = length - 1 | ||||
|
||||
while index_low <= index_high | ||||
middle = (index_low + index_high) / 2 | ||||
if array[middle] == value_to_find | ||||
return true | ||||
elsif value_to_find < middle | ||||
index_high = middle - 1 | ||||
elsif value_to_find > middle | ||||
index_low = middle + 1 | ||||
end | ||||
end | ||||
|
||||
return false | ||||
end | ||||
|
||||
# Helper method provided to sort the array in ascending order | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍