-
Notifications
You must be signed in to change notification settings - Fork 53
Arrays-assignment #30
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 1 commit
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'restricted_array.rb' | ||
| # RestrictedArray can be created using a specified size, or a random size in | ||
| # the range of 1-20 will be chosen for you. | ||
|
|
@@ -9,53 +11,109 @@ | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def length(array) | ||
| raise NotImplementedError | ||
| # array[index] | ||
| i = 0 | ||
| i += 1 until array[i].nil? | ||
|
|
||
| i | ||
idago123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # raise NotImplementedError | ||
| end | ||
|
|
||
| # Prints each integer values in the array | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| 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 | ||
| # raise NotImplementedError | ||
| i = 0 | ||
| until array[i].nil? | ||
| print 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: ? | ||
| def search(array, length, value_to_find) | ||
| raise NotImplementedError | ||
| end | ||
| def search(array, _length, value_to_find) | ||
| # raise NotImplementedError | ||
| i = 0 | ||
| until array[i] == nil | ||
| if array[i] == value_to_find | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
idago123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # Finds and returns the largest integer value the array | ||
| # Assumes that the array is not sorted. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def find_largest(array, length) | ||
| raise NotImplementedError | ||
| def find_largest(array, _length) | ||
|
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 | ||
| largest = array[i] | ||
| until array[i] == nil | ||
| if array[i] > largest | ||
| largest = array[i] | ||
| end | ||
| i = i + 1 | ||
| end | ||
| return largest | ||
| end | ||
|
|
||
| # Finds and returns the smallest integer value in the array | ||
| # Assumes that the array is not sorted. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def find_smallest(array, length) | ||
| raise NotImplementedError | ||
| def find_smallest(array, _length) | ||
|
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 | ||
| smallest = array[i] | ||
| until array[i] == nil | ||
| if array[i] < smallest | ||
| smallest = array[i] | ||
| end | ||
| i = i + 1 | ||
| end | ||
| return smallest | ||
|
|
||
| end | ||
|
|
||
| # Reverses the values in the integer array in place | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse(array, length) | ||
|
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 | ||
| max = length - 1 | ||
| temp = 0 | ||
| until i > max | ||
| temp = array[i] | ||
| array[i] = array[max] | ||
| array[max] = temp | ||
| i = i + 1 | ||
| max = max - 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: ? | ||
| def binary_search(array, length, value_to_find) | ||
|
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 | ||
| i = 0 | ||
| until array[i] == nil | ||
| if array[i] == value_to_find | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| # | ||
idago123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # Helper method provided to sort the array in ascending order | ||
|
|
@@ -75,18 +133,16 @@ def binary_search(array, length, value_to_find) | |
| def sort(array, length) | ||
| length.times do |index| # outer loop - n elements | ||
| min_index = index # assume index is where the next minimally value is | ||
| temp_index = index+1 # compare with values at index+1 to length-1 | ||
| temp_index = index + 1 # compare with values at index+1 to length-1 | ||
| while temp_index < length # inner loop - n-1 elements | ||
| if array[temp_index] < array[min_index] # found a new minimum, update min_index | ||
| min_index = temp_index | ||
| end | ||
| min_index = temp_index if array[temp_index] < array[min_index] # found a new minimum, update min_index | ||
| temp_index += 1 # move to next index | ||
| end | ||
| if min_index != index # next minimum value is not at current index, swap | ||
| temp = array[min_index] | ||
| array[min_index] = array[index] | ||
| array[index] = temp | ||
| end | ||
| next unless min_index != index # next minimum value is not at current index, swap | ||
|
|
||
| temp = array[min_index] | ||
| array[min_index] = array[index] | ||
| array[index] = temp | ||
| end | ||
| end | ||
| ## --- END OF METHODS --- | ||
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.
👍