Skip to content
Open
Changes from all commits
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
108 changes: 100 additions & 8 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require_relative 'restricted_array.rb'
# Minitest::Reporters.use!
# Minitest::Reporters::SpecReporter.new

# RestrictedArray can be created using a specified size, or a random size in
# the range of 1-20 will be chosen for you.
# All values are integers in the range of 1-221.
Expand All @@ -8,54 +11,143 @@
# The restricted_array is terminated by 'nil' i.e. array[length] = nil
# Time complexity: ?
# Space complexity: ?
# no each
def length(array)
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
index = 0
until array[index] == nil
index += 1
end
length = index
return length
end
#length(RestrictedArray.new(6)) #call method and pass a new array with 6 elements?

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# call on the length method to print
# wy can't I see this test fail message when rake
def print_array(array)
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
# print_array([1,2,3]) => 3 #length - 1
# print_array([1,2,3]) => print 2
length = length(array) # how does it know to call on the method above?
index = 0
until array[index] == nil # until array[length -1]
print array[index]#.to_s
index += 1
end
print array[length-1]
end

#print_array(RestrictedArray.new(10))

# 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)
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
#where do you want me to use length here? Christopher
index = 0
#value_to_find = array[index]# can't assign it it would always find it
until array[index] == nil

if array[index] == value_to_find
return true
end
index += 1
end
#print length #it lights up
#p "are you getting here?"
return false
#raise NotImplementedError
end

# Finds and returns the largest integer value the array
# Assumes that the array is not sorted.
# Assumes that the array is not sorted #linear search
# Time complexity: ?
# Space complexity: ?
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
index = 0
largest = 0#array[0]#array[index]
#raise NotImplementedError
until array[index] == nil
if array[index] > largest
largest = array[index]
end
index += 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)
Comment on lines 84 to 86
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
index = 0
smallest = array[index]
#raise NotImplementedError
until array[index] == nil
if array[index] < smallest
smallest = array[index]
end
index += 1
end
return smallest
end
#why is the test expecting the largest number

# 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
#raise NotImplementedError
index = 0
index_last = length-1

until index == index_last || index > index_last#even
hold = array[index]
array[index] = array[index_last]
index += 1
array[index_last] = hold
index_last -= 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)
Comment on lines 121 to 123
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , some indentation improvements could be done here.

raise NotImplementedError
#raise NotImplementedError
# similar logic to setting up unit tests arrange given this then I will do this code to expect this return
#if length < 1 you cannot effectively run a binary search
if length < 1
exit
end
Comment on lines +127 to +129
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if length < 1
exit
end
if length < 1
exit
end


#index = 0
#mid = length(array)/2 #what about thi approach? replace mid with potential_value
min = 0 #array[index]
max = length-1 #array[length-1]

until min > max #until array[index] == nil?
potential_value = ((min+max)/2) #first division # mid = ((min+max)/2)

if array[potential_value] == value_to_find #array[mid] == value_to_find
return true
elsif
array[potential_value] < value_to_find #array[mid] < value_to_find
min = potential_value + 1 # min = mid +1
elsif
array[potential_value] > value_to_find #array[mid] > value_to_find
max = potential_value - 1 # max = mid -1
end

end
return false #placement
end

# Helper method provided to sort the array in ascending order
Expand Down