From a3ca6f8a0602619b086d9b817113ab61de722010 Mon Sep 17 00:00:00 2001 From: Morgan Schuler Date: Mon, 16 Sep 2019 20:40:49 -0700 Subject: [PATCH 1/2] finished first method --- lib/practice_exercises.rb | 30 +++++++++++++++++++++++------ test/practice_exercises_test.rb | 34 ++++++++++++++++----------------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..e89540a 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -2,12 +2,30 @@ # Time Complexity: ? # Space Complexity: ? def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + new_array = [] + list.each do |i| + unless new_array[-1] == i + new_array << i + end + end + return new_array end -# Time Complexity: ? -# Space Complexity: ? -def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" -end +# # Time Complexity: ? +# # Space Complexity: ? +# def longest_prefix(strings) +# raise NotImplementedError +# end + +# list = [] +# list.each do |e| +# unless e == list.include?(e) +# new_array << e +# end +# list.each do |i| +# count = 0 +# if list[count] == list[i+1] +# count += 1 +# end +# end \ No newline at end of file diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 4d0bc10..f32e81a 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -15,29 +15,29 @@ end end - describe "Longest valid substring" do - it "will work for the README strings" do - strings = ["flower","flow","flight"] + # describe "Longest valid substring" do + # it "will work for the README strings" do + # strings = ["flower","flow","flight"] - output = longest_prefix(strings) + # output = longest_prefix(strings) - expect(output).must_equal "fl" - end + # 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"] + # it "will work for the strings with the common prefix in the rear" do + # strings = ["flower","flow","flight", "fpastafl"] - output = longest_prefix(strings) + # output = longest_prefix(strings) - expect(output).must_equal "f" - end + # expect(output).must_equal "f" + # end - it "will work for the README strings" do - strings = ["dog","racecar","car"] + # it "will work for the README strings" do + # strings = ["dog","racecar","car"] - output = longest_prefix(strings) + # output = longest_prefix(strings) - expect(output).must_equal "" - end - end + # expect(output).must_equal "" + # end + # end end From 8397e922409193bde66fd35024172b382d61bb6d Mon Sep 17 00:00:00 2001 From: Morgan Schuler Date: Mon, 16 Sep 2019 20:50:36 -0700 Subject: [PATCH 2/2] Updated method 2 --- lib/practice_exercises.rb | 31 ++++++++++++------------------ test/practice_exercises_test.rb | 34 ++++++++++++++++----------------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index e89540a..7fdcde0 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,6 +1,6 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def remove_duplicates(list) new_array = [] list.each do |i| @@ -11,21 +11,14 @@ def remove_duplicates(list) return new_array end -# # Time Complexity: ? -# # Space Complexity: ? -# def longest_prefix(strings) -# raise NotImplementedError -# end - -# list = [] -# list.each do |e| -# unless e == list.include?(e) -# new_array << e -# end +# Time Complexity: O(n) +# Space Complexity: O(1) +def longest_prefix(strings) + min = strings.min + max = strings.max + string_pre = min.size.times do |i| + break i if min[i] != max[i] + end + min[0...string_pre] +end -# list.each do |i| -# count = 0 -# if list[count] == list[i+1] -# count += 1 -# end -# end \ No newline at end of file diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index f32e81a..4d0bc10 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -15,29 +15,29 @@ end end - # describe "Longest valid substring" do - # it "will work for the README strings" do - # strings = ["flower","flow","flight"] + describe "Longest valid substring" do + it "will work for the README strings" do + strings = ["flower","flow","flight"] - # output = longest_prefix(strings) + output = longest_prefix(strings) - # expect(output).must_equal "fl" - # end + 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"] + it "will work for the strings with the common prefix in the rear" do + strings = ["flower","flow","flight", "fpastafl"] - # output = longest_prefix(strings) + output = longest_prefix(strings) - # expect(output).must_equal "f" - # end + expect(output).must_equal "f" + end - # it "will work for the README strings" do - # strings = ["dog","racecar","car"] + it "will work for the README strings" do + strings = ["dog","racecar","car"] - # output = longest_prefix(strings) + output = longest_prefix(strings) - # expect(output).must_equal "" - # end - # end + expect(output).must_equal "" + end + end end