-
Notifications
You must be signed in to change notification settings - Fork 53
Kal restricted arrays #43
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,117 @@ | |
|
|
||
| # 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 | ||
| index = 0 | ||
| while array[index] != nil | ||
| index += 1 | ||
| end | ||
| return index | ||
| 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
+20
to
22
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 | ||
| index = 0 | ||
| while array[index] != nil | ||
| print array[index] | ||
| index += 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
+32
to
34
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 | ||
| index = 0 | ||
| while index < length | ||
| if array[index] == value_to_find | ||
| return true | ||
| end | ||
| index += 1 | ||
| 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
+47
to
49
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 | ||
| index = 0 | ||
| largest_value = array[0] | ||
|
|
||
| while index < length | ||
| if array[index] > largest_value | ||
| largest_value = array[index] | ||
| end | ||
| index += 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) | ||
| raise NotImplementedError | ||
| index = 0 | ||
| smallest_value = array[0] | ||
|
|
||
| while index < length | ||
| if array[index] < smallest_value | ||
| smallest_value = array[index] | ||
| end | ||
| index += 1 | ||
| end | ||
|
|
||
| return smallest_value | ||
| end | ||
|
|
||
| # Reverses the values in the integer array in place | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: 0(n) | ||
| # Space complexity: 0(1) | ||
| def reverse(array, length) | ||
|
Comment on lines
+82
to
84
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 | ||
| #raise NotImplementedError | ||
| f_index = 0 | ||
| l_index = length - 1 | ||
|
|
||
| while f_index < l_index | ||
| temp = array[f_index] | ||
| array[f_index] = array[l_index] | ||
| array[l_index] = temp | ||
|
|
||
| f_index += 1 | ||
| l_index -= 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)) #still need to have a better understanding of this | ||
| # Space complexity: O(1) | ||
| def binary_search(array, length, value_to_find) | ||
|
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 | ||
| #raise NotImplementedError | ||
| 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 | ||
| 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.
👍