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

Reverse Sentence - Hannah #9

Open
wants to merge 1 commit 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
48 changes: 47 additions & 1 deletion lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
# A method to reverse the words in a sentence, in place.
def reverse_sentence(my_sentence)
raise NotImplementedError
string_reverse(my_sentence)
reverse_words(my_sentence)
end

def string_reverse(my_string)
return my_string if my_string.nil?
return my_string if my_string.length <= 1

i = 0
j = my_string.length - 1
b = 0
while i < j
b = my_string[i]
my_string[i] = my_string[j]
my_string[j] = b
i += 1
j -= 1
end
return my_string
end

def reverse_words(my_words)

Choose a reason for hiding this comment

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

I've the same comments as the ones I added to Ada-C10/reverse_words#24 (comment)

And accordingly, you may want to revisit the algorithm and the complexities for this assignment.

#split string into array of words
return my_words if my_words.nil?

new_string = ""
var = ""

my_words.length.times do |x|
if my_words[x] != " "
var += my_words[x]
else
new_string += string_reverse(var)
new_string += my_words[x]
var = ""
end
end
new_string += string_reverse(var)

my_words.length.times do |x|
my_words[x] = new_string[x]
end
return my_words
end

test_string = "I like tomatoes"

print reverse_sentence(test_string)