Skip to content

Conversation

@CEsGutierrez
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

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

Overall nice work, you hit the learning goals here. Well done. Check my comments below especially with regard to time/space complexity. Let me know if you have questions.

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

Choose a reason for hiding this comment

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

This is not recursive 🤔

return s
end

reversed_str = reverse(s[1..-1])

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.

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

Choose a reason for hiding this comment

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

  1. Recursion means you have some space complexity by the system stack.
  2. You are creating a new string with each recursive call so this is actually O(n2)

# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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 is not in place
  2. This is doing the same as the above method.

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

Choose a reason for hiding this comment

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

👍 But you're not taking the stack into account for space complexity.

# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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 due to creating new arrays.

elsif array[0] == value
return true
else
array.delete_at(0)

Choose a reason for hiding this comment

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

delete_at(0) makes all the elements shift over 1 index costing O(n) for just this one line.

# Time complexity: O(n)
# Space complexity: O(1)

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 due to deleting elements.

# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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 due to creating new arrays.

# Time complexity: O(n + m)
# Space complexity: O(1)

def digit_match(n, m, c = 0)

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 issues with the above methods due to using chop.

You're also not considering the cost of the stack in space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants