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

Finished the assignment #49

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.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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.

102 changes: 79 additions & 23 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,113 @@
# RestrictedArray cannot be resized.

# 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: ?
# The restricted_array is terminated by 'nil' i.e. array[length] = ni
#
# Time complexity: Linear or O(n) --> it will iterate over each of the elements inside array. As the input grows, the greater the amount of time it takes to perform the function.
# Space complexity: Constant of O(1) --> we only create one new variable and it doesnt depend on the size of the array.

def length(array)
Comment on lines +10 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
index = 0
while array[index] != nil do
index += 1
end
return index
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# Time complexity: Linear or O(n) --> it will iterate over each of the elements inside array. As the input grows, the greater the amount of time it takes to perform the function.
# Space complexity: Constant of O(1) -> only one variable is created and its used throughout the entire function.
def print_array(array)
Comment on lines +22 to 24
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
end
i = 0
while array[i] != nil
i += 1
end
return i
end
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
end
end



# For an unsorted array, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: ?
# Space complexity: ?
# Time complexity: Linear or O(n) --> it will need to iterate over all elements in worse case, which means it will grow through each iteration.
# Space complexity: Constant or O(1)
def search(array, length, value_to_find)
Comment on lines +35 to 37
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 index == length
if array[index] == value_to_find
return true
end
index += 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: Linear or O(n)
# Space complexity: Constant or O(1)
def find_largest(array, length)
Comment on lines +50 to 52
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 = 0
index = 0
until index == length
if array[index] > max
max = array[index]
end
index += 1
end
return max
end

# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
# Time complexity: Linear or O(n) --> this would assume that we have to check all elements in array which means it will grow through each iteration.
# Space complexity: Constant of O(1) - two variables are created and are used throughout the function
def find_smallest(array, length)
raise NotImplementedError
min = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the array has positive numbers starting min at 0, will cause these to fail.

Suggested change
min = 0
min = array[0]

index = 0
until index == length
if array[index] < min
min = array[index]
end
index += 1
end
return min
end

# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
# Time complexity: Linear or O(n)
# Space complexity: Constant or O(1)
def reverse(array, length)
Comment on lines +81 to 83
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
half = length/2
i = 0
until i == half do
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp
i += 1
end
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: Logarithmic or O(log n) - I remembered from lessons that binary search is 0(log n)
# Space complexity: Constant or O(1)?
def binary_search(array, length, value_to_find)
Comment on lines +96 to 98
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
lowest = 0
highest = length -1
mid = length/2

while lowest <= highest do
mid = (lowest + highest)/2

if array[mid] == value_to_find
return true
elsif array[mid] < value_to_find
lowest = mid +1
else
highest = mid - 1
end
end
return false
end

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