-
Notifications
You must be signed in to change notification settings - Fork 47
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
Maryam's reverse words #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,61 @@ | ||
# A method to reverse each word in a sentence, in place. | ||
# # A method to reverse each word in a sentence, in place. | ||
require 'pry' | ||
|
||
def reverse_one_word(word, start = 0, finish = word.length) | ||
if word == nil | ||
return nil | ||
end | ||
|
||
i = start | ||
index = finish | ||
|
||
while i < index | ||
first_letter = word[i] | ||
last_letter = word[index] | ||
|
||
word[i] = last_letter | ||
word[index] = first_letter | ||
|
||
i += 1 | ||
index -= 1 | ||
end | ||
|
||
return word | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're reversing in place, you don't need to return word. Line 23 could simply be |
||
end | ||
|
||
def reverse_words(my_words) | ||
raise NotImplementedError | ||
if my_words == nil | ||
return nil | ||
end | ||
|
||
current_index = 0 | ||
|
||
start = nil | ||
finish = nil | ||
|
||
while current_index < my_words.length | ||
currently_on_letter = my_words[current_index] != " " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i haven't seen this before - is it just returning true or false? |
||
currently_on_space = my_words[current_index] == " " | ||
at_end_of_string = current_index == my_words.length - 1 | ||
|
||
if start == nil && currently_on_letter | ||
start = current_index | ||
end | ||
|
||
if start && currently_on_space | ||
finish = current_index - 1 | ||
end | ||
|
||
if start && at_end_of_string | ||
finish = current_index | ||
end | ||
|
||
if start && finish | ||
reverse_one_word(my_words, start, finish) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like that you split it into different functions - easy to follow |
||
start = nil | ||
finish = nil | ||
end | ||
|
||
current_index += 1 | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea for further improvement: can you swap using only one temporary variable instead of two as you have now?