Skip to content

Amy Cash - Pipes - String Manipulation Practice Exercises#19

Open
cashmonger wants to merge 5 commits intoAda-C8:masterfrom
cashmonger:master
Open

Amy Cash - Pipes - String Manipulation Practice Exercises#19
cashmonger wants to merge 5 commits intoAda-C8:masterfrom
cashmonger:master

Conversation

@cashmonger
Copy link

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.

Question Answer
What is the time complexity of the string_reverse method? Provide justification. n -- each letter is considered in turn, so as the string grows, the number of steps will grow linearly.
What is the space complexity of the string_reverse method? Provide justification. Constant. Any additional memory needed isn't dependent on the length of the string.
What is the time complexity of the reverse_words method? Provide justification. n -- the method goes through every letter in the string once, so the number of steps grows linearly.
What is the space complexity of the reverse_words method? Provide justification. Constant. No new memory needs to be allocated once the method begins.
What is the time complexity of the reverse_sentence method? Provide justification. n -- it runs through the two previous methods one time each. Each of those is n, so it's 2 n, which is n.
What is the space complexity of the reverse_sentence method? Provide justification. Constant. No new memory needs to be allocated once the method begins.
What is the time complexity of the palindrome_check method? Provide justification. n -- it checks each letter once to see if it matches the corresponding letter, so as the sentence grows longer, the number of checks that need to be done increases linearly
What is the space complexity of the palindrome_check method? Provide justification. Constant. No new memory needs to be allocated once the method begins.
What is the time complexity of the encode_repeating method? Provide justification. n -- each set of letters goes through the same set of steps. So if there are more sets of letters, the time take increases linearly.
What is the space complexity of the encode_repeating method? Provide justification. Constant. No new memory needs to be allocated once the method begins.

i += 1
j -= 1
end
return reversed_string

Choose a reason for hiding this comment

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

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]

Choose a reason for hiding this comment

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

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)

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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.

@shrutivanw
Copy link

Looks good! See some minor comments and suggestions for improvements in line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants