Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions .idea/restricted-arrays.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 76 additions & 20 deletions lib/using_restricted_array.rb
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.
Expand All @@ -9,53 +11,109 @@
# Time complexity: ?
# Space complexity: ?
def length(array)
Comment on lines 11 to 13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
# array[index]
i = 0
i += 1 until array[i].nil?

i
# raise NotImplementedError
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
def print_array(array)
Comment on lines 23 to 25
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This isn't working. Take a look at my suggestion below.

raise NotImplementedError
#raise NotImplementedError
i = 0
until array[i] == nil
if array[i] == value_to_find
return true
else
return false
end
end
#
end

# Helper method provided to sort the array in ascending order
Expand All @@ -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 ---