From f42a3aa6dfce8928035df14ce7459787c6caaa37 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Fri, 13 Sep 2019 09:37:11 -0700 Subject: [PATCH 1/2] changed test to handle in-place requirement --- test/practice_exercises_test.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 11d820b..b21d143 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -9,34 +9,34 @@ it "works for empty arrays" do expect(remove_duplicates([])).must_equal [] end - + it "will remove duplicates for longer arrays" do - expect(remove_duplicates([1, 2, 2, 3, 3, 4])).must_equal [1, 2, 3, 4] + expect(remove_duplicates([1, 2, 2, 3, 3, 4])).reject{|num| num == nil }).must_equal [1, 2, 3, 4] end end - + describe "Longest valid substring" do it "will work for the README strings" do strings = ["flower","flow","flight"] - + output = longest_prefix(strings) - + expect(output).must_equal "fl" end - + it "will work for the strings with the common prefix in the rear" do strings = ["flower","flow","flight", "fpastafl"] - + output = longest_prefix(strings) - + expect(output).must_equal "f" end - + it "will work for the README strings" do strings = ["dog","racecar","car"] - + output = longest_prefix(strings) - + expect(output).must_equal "" end end From 37228fb64f3dfd660c609024e4c0abeec4a13ad5 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sun, 15 Sep 2019 23:29:44 -0700 Subject: [PATCH 2/2] Completed both methods --- lib/practice_exercises.rb | 33 +++++++++++++++++++++++++++------ test/practice_exercises_test.rb | 11 ++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..5cf6354 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,34 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the sorted array +# Space Complexity: O(1) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + counter = 0 + list.length.times do |num| + if list[counter] == list[counter + 1] + list.delete_at(counter + 1) + else + counter += 1 + end + end + return list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the strings array +# Space Complexity: O(1) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + longest_prefix = strings[0] + strings.each do |string| + letter_counter = 0 + longest_prefix.length.times do + if longest_prefix[letter_counter] == string[letter_counter] + letter_counter += 1 + else + longest_prefix = longest_prefix.slice(0, letter_counter) + break + end + end + end + return longest_prefix end + diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index b21d143..fc16b47 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -11,7 +11,7 @@ end it "will remove duplicates for longer arrays" do - expect(remove_duplicates([1, 2, 2, 3, 3, 4])).reject{|num| num == nil }).must_equal [1, 2, 3, 4] + expect(remove_duplicates([1, 2, 2, 3, 3, 4]).reject{|num| num == nil }).must_equal [1, 2, 3, 4] end end @@ -24,6 +24,14 @@ expect(output).must_equal "fl" end + it "will work for the strings that are the same" do + strings = ["flower","flower","flower"] + + output = longest_prefix(strings) + + expect(output).must_equal "flower" + end + it "will work for the strings with the common prefix in the rear" do strings = ["flower","flow","flight", "fpastafl"] @@ -32,6 +40,7 @@ expect(output).must_equal "f" end + it "will work for the README strings" do strings = ["dog","racecar","car"]