From 0f07c5c4b15cac7b824bd3c21eeedeccc59a81f9 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Tue, 24 Sep 2019 01:18:35 -0700 Subject: [PATCH 1/2] completed wave 1 --- lib/sort_by_length.rb | 27 ++++++++++++++++--- ...rt_by_length.rb => sort_by_length_test.rb} | 0 2 files changed, 24 insertions(+), 3 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (100%) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..ee05726 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -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) + def sort_by_length(my_sentence) - 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") diff --git a/test/sort_by_length.rb b/test/sort_by_length_test.rb similarity index 100% rename from test/sort_by_length.rb rename to test/sort_by_length_test.rb From 532b84c03103523ceb1eb6dd61c7457d33560ae2 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Tue, 24 Sep 2019 18:41:29 -0700 Subject: [PATCH 2/2] wave2 completed --- .vscode/launch.json | 14 ++++++++++++++ lib/reverse_sentence.rb | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e015b31 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..008b371 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,35 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: o(n^2) +# 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) + while i < j + temp = sentence[i] + sentence[i] = sentence[j] + sentence[j] = temp + + i += 1 + j -= 1 + + end +end \ No newline at end of file