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

Maryam's reverse words #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
61 changes: 59 additions & 2 deletions lib/reverse_words.rb
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

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?

word[index] = first_letter

i += 1
index -= 1
end

return word

Choose a reason for hiding this comment

The 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 return

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] != " "

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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
2 changes: 2 additions & 0 deletions specs/reverse_words_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/reverse_words'
require 'pry'

describe "reverse words" do
describe "basic tests" do
Expand All @@ -23,6 +24,7 @@

# check for edge cases
describe "edge cases" do

# if it's a string parameter, check for empty
it "reverse each word in an empty sentence" do
test_string = ""
Expand Down