Skip to content

Ports - Kasey#14

Open
kaseea wants to merge 1 commit intoAda-C11:masterfrom
kaseea:master
Open

Ports - Kasey#14
kaseea wants to merge 1 commit intoAda-C11:masterfrom
kaseea:master

Conversation

@kaseea
Copy link
Copy Markdown

@kaseea kaseea commented May 27, 2019

No description provided.

Copy link
Copy Markdown

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

Nice work, I left some notes, but you did a good job writing the methods. You do need to know that creating a string does add some time and space complexity to solutions.

Let me know if you have questions.

Comment thread lib/recursive-methods.rb

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n) where n is the length of the string
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since s[0...s.length-1] creates a new string, this algorithm is actually O(n^2)

Comment thread lib/recursive-methods.rb
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return s if s == ""
return s[-1] + reverse_inplace(s[0...-1])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is creating new strings, so it isn't in place.

To do it in place you need to swap characters one at a time without creating a new string.

Comment thread lib/recursive-methods.rb
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1) (or O(n) cause it has that count to create and return?)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you have a system stack, it's O(n)

Comment thread lib/recursive-methods.rb

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) (well, O(n/2))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Due to creating a new string it's actually O(n^2)

Comment thread lib/recursive-methods.rb
raise NotImplementedError, "Method not implemented"
if array.length == 0
return false
elsif array.length >= 1 && array[0] == value
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You don't need the array.length >= 1

Comment thread lib/recursive-methods.rb

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

O(n^2)

Comment thread lib/recursive-methods.rb

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

O(n^2)

Comment thread lib/recursive-methods.rb
# Space complexity: O(1)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
if (s.length == 3 && s[0] == s[-1]) || (s.length == 2 && s[0] == s[-1]) || s.length == 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This could probably be simplified to return true if s.length <= 1

Comment thread lib/recursive-methods.rb
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Space complexity is O(n) due to the system stack 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