-
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
Finished the assignment #49
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,57 +5,113 @@ | |||||
# RestrictedArray cannot be resized. | ||||||
|
||||||
# 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: ? | ||||||
# The restricted_array is terminated by 'nil' i.e. array[length] = ni | ||||||
# | ||||||
# Time complexity: Linear or O(n) --> it will iterate over each of the elements inside array. As the input grows, the greater the amount of time it takes to perform the function. | ||||||
# Space complexity: Constant of O(1) --> we only create one new variable and it doesnt depend on the size of the array. | ||||||
|
||||||
def length(array) | ||||||
raise NotImplementedError | ||||||
index = 0 | ||||||
while array[index] != nil do | ||||||
index += 1 | ||||||
end | ||||||
return index | ||||||
end | ||||||
|
||||||
# Prints each integer values in the array | ||||||
# Time complexity: ? | ||||||
# Space complexity: ? | ||||||
# Time complexity: Linear or O(n) --> it will iterate over each of the elements inside array. As the input grows, the greater the amount of time it takes to perform the function. | ||||||
# Space complexity: Constant of O(1) -> only one variable is created and its used throughout the entire function. | ||||||
def print_array(array) | ||||||
Comment on lines
+22
to
24
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 | ||||||
end | ||||||
i = 0 | ||||||
while array[i] != nil | ||||||
i += 1 | ||||||
end | ||||||
return i | ||||||
end | ||||||
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.
Suggested change
|
||||||
|
||||||
|
||||||
# For an unsorted array, searches for 'value_to_find'. | ||||||
# Returns true if found, false otherwise. | ||||||
# Time complexity: ? | ||||||
# Space complexity: ? | ||||||
# Time complexity: Linear or O(n) --> it will need to iterate over all elements in worse case, which means it will grow through each iteration. | ||||||
# Space complexity: Constant or O(1) | ||||||
def search(array, length, value_to_find) | ||||||
Comment on lines
+35
to
37
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 | ||||||
until 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: Linear or O(n) | ||||||
# Space complexity: Constant or O(1) | ||||||
def find_largest(array, length) | ||||||
Comment on lines
+50
to
52
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 | ||||||
max = 0 | ||||||
index = 0 | ||||||
until index == length | ||||||
if array[index] > max | ||||||
max = array[index] | ||||||
end | ||||||
index += 1 | ||||||
end | ||||||
return max | ||||||
end | ||||||
|
||||||
# Finds and returns the smallest integer value in the array | ||||||
# Assumes that the array is not sorted. | ||||||
# Time complexity: ? | ||||||
# Space complexity: ? | ||||||
# Time complexity: Linear or O(n) --> this would assume that we have to check all elements in array which means it will grow through each iteration. | ||||||
# Space complexity: Constant of O(1) - two variables are created and are used throughout the function | ||||||
def find_smallest(array, length) | ||||||
raise NotImplementedError | ||||||
min = 0 | ||||||
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. If the array has positive numbers starting
Suggested change
|
||||||
index = 0 | ||||||
until index == length | ||||||
if array[index] < min | ||||||
min = array[index] | ||||||
end | ||||||
index += 1 | ||||||
end | ||||||
return min | ||||||
end | ||||||
|
||||||
# Reverses the values in the integer array in place | ||||||
# Time complexity: ? | ||||||
# Space complexity: ? | ||||||
# Time complexity: Linear or O(n) | ||||||
# Space complexity: Constant or O(1) | ||||||
def reverse(array, length) | ||||||
Comment on lines
+81
to
83
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 | ||||||
half = length/2 | ||||||
i = 0 | ||||||
until i == half do | ||||||
temp = array[i] | ||||||
array[i] = array[length - 1 - i] | ||||||
array[length - 1 - i] = temp | ||||||
i += 1 | ||||||
end | ||||||
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: Logarithmic or O(log n) - I remembered from lessons that binary search is 0(log n) | ||||||
# Space complexity: Constant or O(1)? | ||||||
def binary_search(array, length, value_to_find) | ||||||
Comment on lines
+96
to
98
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 | ||||||
lowest = 0 | ||||||
highest = length -1 | ||||||
mid = length/2 | ||||||
|
||||||
while lowest <= highest do | ||||||
mid = (lowest + highest)/2 | ||||||
|
||||||
if array[mid] == value_to_find | ||||||
return true | ||||||
elsif array[mid] < value_to_find | ||||||
lowest = mid +1 | ||||||
else | ||||||
highest = 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.
👍