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

Leaves - Emily V #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

emilyvomacka
Copy link

Sorry this is a bit late--catching up from a weekend out of town. Recursion is fun :)

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.

# 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.

👍

def reverse(s, pointer = s.length - 1)
return s if s == ""
return s[pointer] if pointer == 0
return s[pointer] + reverse(s, pointer - 1)

Choose a reason for hiding this comment

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

String concatenation creates a new array and copies all the individual elements over and so is O(n) by itself.

raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), where n is the length of the string
def reverse(s, pointer = s.length - 1)

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"
# Time complexity: O(n), where n is the length of the string. (More precisely, n/2)
# Space complexity: O(n), where n is the length of the string. (More precisely, n/2)
def reverse_inplace(s, start = 0, finish = s.length - 1)

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 the same time/space complexity as the method above.

# 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"
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), where n is the length of the string
def nested(s, parens_counter = 0, pointer = 0)

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"
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), where n is the length of the string
def search(array, value, pointer = 0)

Choose a reason for hiding this comment

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

👍

def is_palindrome(s, length = s.length, pointer = 0)
return true if pointer > length / 2
return false if s[pointer] != s[length - 1 - pointer]
return is_palindrome(s, length, pointer + 1) if s[pointer] == s[length - 1 - pointer]

Choose a reason for hiding this comment

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

You shouldn't need the if here.

Suggested change
return is_palindrome(s, length, pointer + 1) if s[pointer] == s[length - 1 - pointer]
return is_palindrome(s, length, pointer + 1)

raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), where n is the length of the string (More precisely, n/2)
# Space complexity: O(n), where n is the length of the string (More precisely, n/2)
def is_palindrome(s, length = s.length, pointer = 0)

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"
# Time complexity: O(n), where n is the length of the shorter string
# Space complexity: O(n), where n is the length of the shorter string
def digit_match(n, m, pointer = -1, matches = 0)

Choose a reason for hiding this comment

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

👍 Clever!

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