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
46 changes: 46 additions & 0 deletions lib/binary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def binary_search(array, to_find)

Choose a reason for hiding this comment

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

huh? 🤔

high = array.length - 1
low = 0
while high >= low
mid = (high + low) / 2
if array[mid] == to_find
return mid
elsif array[mid] > to_find
high = mid - 1
else
low = mid + 1
end
end

return nil
end

def recursive_binary_search(array, to_find, low = 0, high = array.length - 1)
mid = (high + low) / 2
return nil if low > high
return array[mid] if array[mid] == to_find
if to_find < array[mid]
return recursive_binary_search(array, to_find, low, mid - 1)
else
return recursive_binary_search(array, to_find, mid + 1, high)
end
end

class List
attr_reader :head, :tail

def initialize(head, tail)
@head = head
@tail = tail
end

def self.from_array(array)

end

def search(list = self)

end


end
72 changes: 46 additions & 26 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def factorial(n)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method not implemented"
return raise ArgumentError if n < 0
return 1 if n == 1 || n == 0
return n * factorial(n-1) if n > 1
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)

Choose a reason for hiding this comment

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

👍
This works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

raise NotImplementedError, "Method not implemented"
return s if s.length == 1 || s.empty?
return s[-1] + reverse(s[1..-2]) + s[0]

Choose a reason for hiding this comment

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

s[1..-1] creates a new array and copies all the individual elements over and so is O(n) by itself.

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) if I had gotten it to work
# Space complexity: O(n)
def reverse_inplace(s)

Choose a reason for hiding this comment

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

  1. This method is not in place because s[1..-2] creates a new array.
  2. It's not working.

consider instead:

def reverse_in_place(s, left = 0, right = s.length -1)
  if left < right
    temp = s[left]
    s[left] = s[right]
    s[right] = temp
    return reverse_in_place(s, left + 1, right - 1)
  end
  return s
end

raise NotImplementedError, "Method not implemented"
return s if s.length == 1 || s.empty? || s[0] == s[-1]
while s.length > 0
x = s[0]
y = s[-1]
s[-1] = x
s[0] = y
return reverse_inplace(s[1..-2])
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method not implemented"
return 0 if n == 0
return 2 + bunny(n - 1) if n >= 1
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s)

Choose a reason for hiding this comment

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

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return true if s.empty?
return nested(s[1..-2]) if s[0] == "(" && s[-1] == ")"
return false
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def search(array, value)

Choose a reason for hiding this comment

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

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return false if array.empty?
return true if array[0] == value
return search(array[1..-1], value)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)

Choose a reason for hiding this comment

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

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return true if s.empty? || s.length == 1
return is_palindrome(s[1..-2])if s[0] == s[-1]
return false
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(log(n))
# Space complexity: O(log(n))
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
return 1 if n == 0 && m == 0
return 0 if n == 0 || m == 0
return 1 if n.digits[0] == 0 && m.digits[0] == 0
return 0 + digit_match(n / 10, m / 10) if n % 10 != m % 10
return 1 + digit_match(n / 10, m / 10) if n % 10 == m % 10
Comment on lines +64 to +68

Choose a reason for hiding this comment

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

This will fail for n = 20 and m = 20.

end
Loading