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

Laura | Nodes #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 44 additions & 2 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
# A method to reverse the words in a sentence, in place.
# A method to reverse the my_sentences in a sentence, in place.
# Time: O(n^2)

Choose a reason for hiding this comment

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

The time complexity of your algorithm is actually O(n). Take a read on an explanation on: https://github.com/Ada-C10/reverse_sentence/blob/solution/lib/reverse_sentence.rb

I'd also encourage you to elaborate your time and space complexity reasoning. Often, being able to explain your logic and reasoning is more important than getting the correct final answer.

# Space: O(1)
def reverse_sentence(my_sentence)
raise NotImplementedError

if my_sentence == nil || my_sentence == "" || my_sentence.length == 1
return my_sentence
end
reverse_string(0, my_sentence.length - 1, my_sentence)
i = 0
s = 0
e = 0

Choose a reason for hiding this comment

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

Minor: your indentation seems a little off.

while i < my_sentence.length
# skip space
while my_sentence[i + 1] == " " || my_sentence[i] == " "
i += 1
end
# define start of sentence
if my_sentence[i] != " "
s = i
end

# find end
# why does it go into an infinite loop with OR
while my_sentence[i+1] != " " && my_sentence[i+1] != nil
i += 1
end

e = i
reverse_string(s, e, my_sentence)
i += 1
end
end

def reverse_string(startw, endw, string)
i = startw
j = endw
while i < j
temp = string[i]
string[i] = string[j]
string[j] = temp
i += 1
j -= 1
end
end