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

Arrays exercise submission @ Water class-Hanh Solo #36

Open
wants to merge 2 commits 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
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.

142 changes: 120 additions & 22 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,153 @@

# 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: ?
# Time complexity: There is only one while loop where we iterate over each
# element of the array. Time complexity will be O(n) time where n is how many
# times the loop will execute.
# Space complexity: The method used one local integer variable
# no matter the input size, so the space complexity is constant or O(1).
def length(array)
Comment on lines +9 to 14
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
while array[i] != nil
i +=1
end
return i
# raise NotImplementedError will never execute as the function stop at return statement

end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# Time complexity: There is only one while loop where we iterate over each
# element of the array. Time complexity will be n time where n is how many
# times the loop will execute.
# Space complexity: constant or O(1)
def print_array(array)
Comment on lines +25 to 29
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
until array[i].nil? # until object == nil
puts 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: ?
# Time complexity:O(n) There is only one while loop where we iterate over each
# element of the array. Time complexity will be n time where n is how many
# times the loop will execute.
# Space complexity: constant or O(1)
def search(array, length, value_to_find)
Comment on lines +39 to 43
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
if length == 0
return false
end

i = 0
while i <= length
if array[i] == value_to_find
return true
end
i += 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:This method will iterate through the entire array. Thus if the
# array size doubles the loop will take twice as long. So the time complexity is O(n)
# Space complexity: This method only creates two additional variables,
# index and max which does not change no matter how large the array gets.
# Therefore the space complexity does not change as the size of the array increases.
# This means the space complexity is O(1).
def find_largest(array, length)
Comment on lines +62 to 68
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
max_value = 0
i = 0
while i < length
if array[i] > max_value
max_value = array[i]
end
i += 1
end

return max_value

end

# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
# Time complexity: This method will iterate through the entire array. Thus if the
# array size doubles the loop will take twice as long. So the time complexity is O(n)
# Space complexity: This method only creates two additional variables,
# index and min which does not change no matter how large the array gets.
# Therefore the space complexity does not change as the size of the array increases.
# This means the space complexity is O(1).
def find_smallest(array, length)
Comment on lines +84 to 90
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
min_value = array[0]
i = 0
while i < length
if array[i] < min_value
min_value = array[i]
end
i += 1
end

return min_value

end

# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
# Time complexity:O(n) this method only iterate through half of the array however
# it is depends on the size of the input. The longer the array, the longer it takes or
# linear complexity.
# Space complexity: constant
def reverse(array, length)
Comment on lines +105 to 109
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
# The array reserves back at the mid index
n = length/2
i = 0

while i < n
temp = array[i]
array[i] = array[length - i - 1]
array[length - i - 1] = temp
i += 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: ?
# Time complexity: Log2 n means at each step the value reduces by half of the remaining.
# It also means that as the size of the array doubles,
# the number of iterations only increases by 1.
# The main loop in binary search runs log2 n number of times where
# n is the number of elements in the input array.
# Space complexity: constant
def binary_search(array, length, value_to_find)
Comment on lines +128 to 134
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
if length == 0
return false
end
# initialized to the index of the first element in the array
first_index = 0
# initialized to the index of the last element in the array
last_index = length - 1

while first_index <= last_index
mid_index = (first_index + last_index)/2
if array[mid_index] == value_to_find
return true
elsif array[mid_index] > value_to_find
last_index = mid_index - 1
elsif array[mid_index] < value_to_find
first_index = mid_index + 1
end
end

return false

end

# Helper method provided to sort the array in ascending order
Expand All @@ -75,7 +172,7 @@ 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
Expand All @@ -89,4 +186,5 @@ def sort(array, length)
end
end
end

## --- END OF METHODS ---