Skip to content

Conversation

@dev-elle-up
Copy link

Implemented def factorial(n) and def revers(s).

Chris, I ended up figuring it out after solving the same problem in Exercism! 👍

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.

Gave you some feedback. Take a look at my notes for reverse_in_place. Not bad with what you've written. Let me know if you have questions.


# e.g. reverse("hello world") will return "dlrow olleh"

# Time complexity: O(n) (I think? I'm not sure on this.)

Choose a reason for hiding this comment

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

Because s[0..-2] creates a new string and you have n recursive calls this will be O(n^2) for both time and space.

return s if (s.length <= 1)
return s[-1] + reverse(s[0..-2])

# CHRIS: Here is the mess of commented out code I warned you about. I ended up figuring it out when doing the Exercism problem this weekend and was able to quickly convert that code to Ruby! :D :D :D Hooray!!

Choose a reason for hiding this comment

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

Think about this,

You can make a helper method reverse_helper which takes the string and left_index and right_index. The base case is when left_index >= right_index. Otherwise you can swap the characters at left_index and right_index, then call the helper recursively adding one to left_index and subtracting one from right_index.

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