Skip to content
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
Empty file added .idea/.gitignore
Empty file.
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.

27 changes: 27 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.

39 changes: 39 additions & 0 deletions .idea/workspace.xml

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

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

# 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: O(n)
# Space complexity: O(1)
def length(array)
Comment on lines +9 to 11
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

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

return i
end

# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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
i = 0

until array[i] == nil
puts 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:
# sorted: O(n)
# unsorted: O(n)
# Space complexity: O(1)
def search(array, length, value_to_find)
Comment on lines +35 to 39
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 i < length
if array[i] == value_to_find
return true
else
i += 1
end
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:
# sorted: O(1)
# unsorted: O(n)
# Space complexity: O(1)
def find_largest(array, length)
Comment on lines +55 to 59
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Actually this algorithm is always O(n) in time complexity

raise NotImplementedError
end
i = 0

if length == 0
return nil
else
max = array[0]

while i < length
if array[i] > max
max = array[i]
end
i += 1
end

return max
end
end

# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
# Time complexity:
# sorted: O(1)
# unsorted: O(n)
# Space complexity: O(1)
def find_smallest(array, length)
Comment on lines +80 to 84
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

if length == 0
return nil
else
min = array[0]
while i < length
if array[i] < min
min = array[i]
end
i += 1
end

return min
end
end

# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def reverse(array, length)
Comment on lines +103 to 105
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , this works but since you're making an auxiliary array it has O(n) space complexity.

Can you think of a way to do this without the other array?

raise NotImplementedError
i = 0
j = length - 1
temp_array = Array.new(length)

while i < length
temp_array[i] = array[j]
i += 1
j -= 1
end

i = 0
while i < length
array[i] = temp_array[i]
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: ?
def binary_search(array, length, value_to_find)
Comment on lines 125 to 127
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

if length == 0
return false
end

low = 0
high = length - 1

while low <= high
mid = (low + high) / 2
# calculate the index value between low and high
if array[mid] == value_to_find
# compare the element at index mid with the value to find
return true
elsif array[mid] > value_to_find
# value to find is less than the value at mid index
# eliminate the second half
high = mid - 1
elsif array[mid] < value_to_find
# value to find is greater than the value at mid index
# eliminate the first half
low = mid + 1
end
end
# value not found in the array
return false
end

# Helper method provided to sort the array in ascending order
Expand Down Expand Up @@ -89,4 +185,4 @@ def sort(array, length)
end
end
end
## --- END OF METHODS ---
# --- END OF METHODS ---