Skip to content
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

Iris Lux #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
89 changes: 82 additions & 7 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,128 @@
# Time complexity: ?
# Space complexity: ?
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
i = 1

until array[i].nil?
i += 1
end

return i
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
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

for i in 0...length(array)
puts array[i]
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)
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

value_found = nil

for i in 0...length
if(array[i] == value_to_find)
value_found = true
break
else
value_found = false
end
end

return value_found
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)
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
largest = array[0]

for i in 1...length

if array[i] > largest
largest = array[i]
end
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)
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
smallest = array[0]

for i in 1...length

if array[i] < smallest
smallest = array[i]
end
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

#swap the first and last elements first
# first index begins at zero, when it gets to the end (which will be the middle) sawp is complete
#
start = 0
finish = length - 1

while start < finish

temp = array[start]
array[start] = array[finish]
array[finish] = temp

start += 1
finish -= 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.

👍

raise NotImplementedError

lower = 0
upper = length
mid = length/2

while value_to_find != array[mid] && upper > lower

mid = lower + (upper - lower) / 2

if array[mid] < value_to_find
lower = mid + 1
end

if array[mid] > value_to_find
upper = mid - 1
end
end
return value_to_find == array[mid]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice bit of boolean zen here.

end

# Helper method provided to sort the array in ascending order
Expand Down
8 changes: 8 additions & 0 deletions test/using_restricted_array_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/pride'
require "minitest/skip_dsl"
require 'pry'

require_relative '../lib/restricted_array'
require_relative '../lib/using_restricted_array'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "restricted array" do
it "length method" do
# Arrange
Expand Down