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

Layla - Edges - reverse sentence #34

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
52 changes: 50 additions & 2 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
# A method to reverse the words in a sentence, in place.
# A method to reverse each word in a sentence, in place.
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.

This method uses four temporary storage spaces to hold position, string_length, start_point and end_point. This is always true regardless of the input my_words (the value of it or the number of characters in it). Hence, the space complexity of this method is O(1) or constant.

To count the number of operations, we look at the nested loops. By going through one of the two inner, until loops, all n characters in my_words are examined and compared. Through the examination the beginning and end indices of a word is identified. Then, reverse_word is called - which reverses the characters in the word by examining the characters in the word one more time. Imagine a scenario where there is only one word in the input string. In this case, all n characters will be examined once to determine that the word begins at index 0 and ends at index string_length-1. Then, reverse_word reverses the characters in the word by swapping n/2 times. In this case there are n+(n/2) operations. Let's consider the other extreme, where there are only spaces in the string. In this case, all n characters will be examined once. reverse_word will not conduct any swaps since start_point will be greater than end_point. So, regardless, the number of operations will be O(n) or linear.

return my_words unless my_words

position = 0
string_length = my_words.length

while position < my_words.length
until my_words[position] != ' ' || position >= string_length
position += 1
end

start_point = position

until my_words[position] == ' ' || position >= string_length
position += 1
end

end_point = position - 1

reverse_word(my_words, start_point, end_point)
end

return
end

def reverse_word(my_words, start_point, end_point)

Choose a reason for hiding this comment

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

This method uses three temporary storage values: two to hold the index values i and j and one to hold the temporary character while swapping. So, regardless of how many characters are in the input or what characters are in the string, this method will always need 3 temporary storage values. Hence, the space complexity is O(1) or constant.

To deduce the time complexity, we count the number of operations in this function as compared to either the input value or input size. In this case, it would depend on the number of characters from start_point to end_point If there are n characters between start_point and end_point then the while loop will run n/2 times or in other words the method will swap characters n/2 times. That's why, the time complexity will be O(n/2) or by dropping the constant, O(n) or linear.


i = start_point
j = end_point

while i < j
temp_char = my_words[i]
my_words[i] = my_words[j]
my_words[j] = temp_char
i += 1
j -= 1
end

return
end

def reverse_sentence(my_sentence)

Choose a reason for hiding this comment

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

This method only allocates two temporary storage spaces to hold the start_point and end_point. This is constant since it does not change based on the number of characters in the input string, my_sentence.

So, the time and space complexity of this method is simply the sum of the time and space complexities of the two methods it calls one after another.

raise NotImplementedError
return my_sentence unless my_sentence

start_point = 0
end_point = my_sentence.length - 1

reverse_word(my_sentence, start_point, end_point)

reverse_words(my_sentence)
end