-
Notifications
You must be signed in to change notification settings - Fork 53
Restricted Arrays - Mackenzie #27
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
base: master
Are you sure you want to change the base?
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,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) | ||||||||||
| 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
Collaborator
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 | ||||||||||
| 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
Collaborator
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 | ||||||||||
| 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
Collaborator
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 | ||||||||||
| 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
Collaborator
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 = 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
Collaborator
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 | ||||||||||
| 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
Collaborator
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 | ||||||||||
| 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
Collaborator
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. Just a little boolean zen
Suggested change
|
||||||||||
| #raise NotImplementedError | ||||||||||
| 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.
👍