Amy Cash - Pipes - String Manipulation Practice Exercises#19
Amy Cash - Pipes - String Manipulation Practice Exercises#19cashmonger wants to merge 5 commits intoAda-C8:masterfrom cashmonger:master
Conversation
| i += 1 | ||
| j -= 1 | ||
| end | ||
| return reversed_string |
There was a problem hiding this comment.
Since you're reversing the string in place, you don't need to return anything. In fact, you can implement this without assigning a new reference, reversed_string to the object that my_string refers to.
| segment = "" | ||
| start = i + 1 | ||
| else | ||
| segment << my_words[i] |
There was a problem hiding this comment.
You are creating new memory here by adding each character in every word to segment. So the current space complexity of your method is O(n) if there is only one word in my_words or O(k) where k is the length of the longest word in the input string. Can you author this method without using this additional space and indeed make your space complexity O(1)?
| # puts "Original: #{sentence}" | ||
| # reversed_sentence = "awesome is Yoda" | ||
| # A method to reverse the words in a sentence, in place. | ||
| def reverse_sentence(my_sentence) |
There was a problem hiding this comment.
Once you update reverse_words to have a space complexity of O(1), the space complexity of this method will also become O(1).
| j = my_phrase.length - 1 | ||
|
|
||
| until i >= j | ||
| if my_phrase[i] == " " |
There was a problem hiding this comment.
Looks great!
As an added optimization, consider making the conditional statements into loops for skipping white spaces in my_phrase - just to make your method even more flexible.
|
Looks good! See some minor comments and suggestions for improvements in line. |
String Manipulation Practice Exercises
Congratulations! You're submitting your assignment.
Comprehension Questions
What is the time and space complexity for each method you implemented? Provide justification.