From 00e41c8671d44e5e11bfd6ad5ca0e8096b188602 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Sat, 21 Sep 2019 16:46:37 -0700 Subject: [PATCH 1/5] passed unit tests for #sort by length method --- lib/sort_by_length.rb | 24 +++++++++++++++++++++--- test/sort_by_length_test.rb | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/sort_by_length_test.rb diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..08a2e60 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,25 @@ # 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" + array = my_sentence.split + length = array.length + i = 0 + + while i < length-1 + j = 0 + while j < length-i-1 + if array[j].length > array[j+1].length # compares the length of each word + temp = array[j] + array[j] = array[j+1] + array[j+1] = temp + end + j += 1 + end + i += 1 + end + + return array end diff --git a/test/sort_by_length_test.rb b/test/sort_by_length_test.rb new file mode 100644 index 0000000..c38d976 --- /dev/null +++ b/test/sort_by_length_test.rb @@ -0,0 +1,19 @@ +require_relative "test_helper" + +describe "sort_by_length" do + it "will return an empty array for an empty string" do + expect(sort_by_length("")).must_equal [] + end + + it "will return an array of words, by length" do + expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"] + end + + it "will return an array of words by length, words that are of equal length will appear in the order they appear" do + expect(sort_by_length("words of equal length")).must_equal ["of", "words", "equal", "length"] + end + + it "will return an array of words by length, words that are of equal length will appear in the order they appear" do + expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"] + end +end \ No newline at end of file From c949f4d76fa133d957070244bb86e45f04c26308 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Mon, 23 Sep 2019 21:25:08 -0700 Subject: [PATCH 2/5] able to reverse the WHOLE string, not just by each word --- .DS_Store | Bin 0 -> 6148 bytes lib/reverse_sentence.rb | 27 ++++++++++++++++++++++++++- lib/sandbox.rb | 15 +++++++++++++++ test/sort_by_length.rb | 19 ------------------- 4 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 .DS_Store create mode 100644 lib/sandbox.rb delete mode 100644 test/sort_by_length.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a5c9ab4283c0e659813705d13df1a34916fcee20 GIT binary patch literal 6148 zcmeHK%}xR_5N-jrV2m70nIsDa%;WRrDE*u{;mn0*a>BcH(6 zai%RsA_q^#=uFanGo6`s=S#Lfj4|Gs4w{Tr8Djz(B1dJ3;P6sc!3HC8oFg9lq4L3c z^~cut+X275$U+t{-%me(e;C9`qw&TI#nQ^^nh+IH*{EE*BbB;|mrUc1H@QUX)JsOb z)z6d)KXlJO4~A*4x^<+Y#0#R~L?;J>A%t9A1kpgH9W{*xv7W~@4k5~-+^g2IY`4{_ zOXy~GnYG)Ey4>G8n9a&!d#8DL(!afb$R1};&xT(P^lRC)IENQdY+0)ZvUf2B{|oh=D~0 zipH$s`F{+*%+g2xVhUNr05R~#7~rL@+wDM6_H6x99-g%lS|1t;#^tDhfWB}E00Z}t fnsyq$L>=NBi@8CZ1;=$dAYBABA=D8AKfu5{0nSgk literal 0 HcmV?d00001 diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..20ece7e 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,31 @@ # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? +require 'pry' def reverse_sentence(my_sentence) - raise NotImplementedError + + unless my_sentence == nil + + # array = my_sentence.split(/\.?\s+/) + # l = array.length + # i = 0 + # # new_array = [] + + # l.times do + # temp = array[l-1] + # array[i] = temp + + # i += 1 + + # l -= 1 + # end + + # return array.join(' ') + + + + # (my_sentence.length/2).times {|i| my_sentence[i], my_sentence[-i-1] = my_sentence[-i-1], my_sentence[i]} + # my_sentence + + end end diff --git a/lib/sandbox.rb b/lib/sandbox.rb new file mode 100644 index 0000000..9580180 --- /dev/null +++ b/lib/sandbox.rb @@ -0,0 +1,15 @@ +def find_smallest(array) + smallest = array[0] + i = 0 + + while i < array.length - 1 + if array[i] < smallest + smallest = array[i] + smallest_index = i + end + smallest_index += 1 + end + return array +end + +p find_smallest([3, 5, 7, 1]) \ No newline at end of file diff --git a/test/sort_by_length.rb b/test/sort_by_length.rb deleted file mode 100644 index c38d976..0000000 --- a/test/sort_by_length.rb +++ /dev/null @@ -1,19 +0,0 @@ -require_relative "test_helper" - -describe "sort_by_length" do - it "will return an empty array for an empty string" do - expect(sort_by_length("")).must_equal [] - end - - it "will return an array of words, by length" do - expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"] - end - - it "will return an array of words by length, words that are of equal length will appear in the order they appear" do - expect(sort_by_length("words of equal length")).must_equal ["of", "words", "equal", "length"] - end - - it "will return an array of words by length, words that are of equal length will appear in the order they appear" do - expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"] - end -end \ No newline at end of file From 6e309fceb97140ab0d6fa93b80543fb415baf8fc Mon Sep 17 00:00:00 2001 From: Diana Han Date: Mon, 30 Sep 2019 00:41:57 -0700 Subject: [PATCH 3/5] unable to pass tests for #reverse_sentence method --- lib/reverse_sentence.rb | 42 ++++++++++++++++------------------- test/reverse_sentence_test.rb | 4 ++-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 20ece7e..1b999f2 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,31 +1,27 @@ # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? -require 'pry' def reverse_sentence(my_sentence) + + if my_sentence == nil + return nil + elsif + my_sentence == "" + return "" + end - unless my_sentence == nil - - # array = my_sentence.split(/\.?\s+/) - # l = array.length - # i = 0 - # # new_array = [] - - # l.times do - # temp = array[l-1] - # array[i] = temp - - # i += 1 - - # l -= 1 - # end - - # return array.join(' ') - - - - # (my_sentence.length/2).times {|i| my_sentence[i], my_sentence[-i-1] = my_sentence[-i-1], my_sentence[i]} - # my_sentence + array = my_sentence.split(/\.?\s+/) + i = 0 + j = array.length - 1 + (array.length / 2).times do + temp = array[i] + array[i] = array[j] + array[j] = temp + i += 1 + j -= 1 end + + my_sentence = array.join + return my_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..d6db88e 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -7,7 +7,7 @@ reverse_sentence(test_string) - test_string.must_equal "world hello," + _(test_string).must_equal "world hello," end it "reverse a sentence with three words" do @@ -15,7 +15,7 @@ reverse_sentence(test_string) - test_string.must_equal "awesome! is Yoda" + _(test_string).must_equal "awesome! is Yoda" end end From 3237d0a24a0db85f9fc5eebc4d50a41e08c0cbd7 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Mon, 30 Sep 2019 00:54:37 -0700 Subject: [PATCH 4/5] removed sandbox.rb --- lib/sandbox.rb | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 lib/sandbox.rb diff --git a/lib/sandbox.rb b/lib/sandbox.rb deleted file mode 100644 index 9580180..0000000 --- a/lib/sandbox.rb +++ /dev/null @@ -1,15 +0,0 @@ -def find_smallest(array) - smallest = array[0] - i = 0 - - while i < array.length - 1 - if array[i] < smallest - smallest = array[i] - smallest_index = i - end - smallest_index += 1 - end - return array -end - -p find_smallest([3, 5, 7, 1]) \ No newline at end of file From 8c5041987e54ebb58ee1fc1907f37fff3ef51c01 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Thu, 3 Oct 2019 19:13:24 -0700 Subject: [PATCH 5/5] worked on reverse_sentence method and all tests now pass --- .DS_Store | Bin 6148 -> 6148 bytes lib/.DS_Store | Bin 0 -> 6148 bytes lib/reverse_sentence.rb | 21 ++++++++------------- test/reverse_sentence_test.rb | 4 ++-- 4 files changed, 10 insertions(+), 15 deletions(-) create mode 100644 lib/.DS_Store diff --git a/.DS_Store b/.DS_Store index a5c9ab4283c0e659813705d13df1a34916fcee20..b9464028fd4872a4d4b5facb6387ab24c613cab6 100644 GIT binary patch delta 21 ccmZoMXffEZl##>K$W%wc($H-4TE=iO07uyd9RL6T delta 21 bcmZoMXffEZl##>8)Ko_SNNirq7%m0?N3;eR diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ace602a7916ed71f6f808ca2c180767e6e6884ca GIT binary patch literal 6148 zcmeHKJxc>Y5Pcg{L~LSbxqmhf5W$H3p1