Skip to content
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
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Local File",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/main.rb"
}
]
}
35 changes: 32 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n^2)

Choose a reason for hiding this comment

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

This is actually O(n), since you don't reverse the string n times, but only smaller words. If you want to be technical it's O(n * m) where m is the length of a word, but since most words are pretty small you can drop that term.

# Space complexity: o(1)
def reverse_sentence(my_sentence)
raise NotImplementedError
if my_sentence == nil
return ""
end

i = 0
j = 0
while j < my_sentence.length
char = my_sentence[j]
if char == " "
reverse(my_sentence, i, j - 1)
i = j + 1
end
j += 1
end

reverse(my_sentence, i, my_sentence.length - 1)
reverse(my_sentence, 0, my_sentence.length - 1)

end

def reverse(sentence, i, j)

Choose a reason for hiding this comment

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

I love the helper method

while i < j
temp = sentence[i]
sentence[i] = sentence[j]
sentence[j] = temp

i += 1
j -= 1

end
end
27 changes: 24 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# A method which will return an array of the words in the string
# sorted by the length of the word.
# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n^2)
# Space complexity: o(n)
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

Correct


def sort_by_length(my_sentence)

Choose a reason for hiding this comment

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

Nice bubblesort.

raise NotImplementedError, "Method not implemented"
words = my_sentence.split(" ")
# print words
#************
length = words.length
i = 0
while i < length - 1
j = 0
while j < length - i - 1
if words[j].length > words[j + 1].length
temp = words[j]
words[j] = words[j + 1]
words[j + 1] = temp
end
j += 1
end
i += 1
end
return words
end


# print sort_by_length("abc ab a")
File renamed without changes.