From 070ba9289e6dffcbfc567b199ddef7fcf694dfd6 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Sun, 15 Sep 2019 23:20:51 -0700 Subject: [PATCH 1/5] Assignment done --- lib/practice_exercises.rb | 50 ++++++++++++++++++++++++++++----- test/practice_exercises_test.rb | 10 +++---- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..78ac209 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,49 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: o(n) +# Space Complexity: o(1) + +# num = [1, 2, 2, 3, 4, 4] def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + i = 0 + while i <= list.length + if list[i] == list[i + 1] + list.delete_at(i) + end + i += 1 + end + + # print list.length + return list end -# Time Complexity: ? -# Space Complexity: ? +# remove_duplicates(num) +#============================================================== + +# Time Complexity: o(n^2) +# Space Complexity: ?o(1) + def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" -end + total_words = strings.length + if total_words == 0 + return "" + end + + if total_words == 1 + return strings[0] + end + longest_prefix = "" + total_words.times do |index| + word = strings[index] + i = index + 1 + while i < total_words + next_word = strings[i] + if word[i] == next_word[i] + longest_prefix += word[i] + break + end + i += 1 + end + end + return longest_prefix +end diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 4d0bc10..4a9108b 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -5,19 +5,19 @@ it "works for 1 element strings" do expect(remove_duplicates([1])).must_equal [1] end - + 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]).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 describe "Longest valid substring" do it "will work for the README strings" do - strings = ["flower","flow","flight"] + strings = ["flower", "flow", "flight"] output = longest_prefix(strings) @@ -25,7 +25,7 @@ end it "will work for the strings with the common prefix in the rear" do - strings = ["flower","flow","flight", "fpastafl"] + strings = ["flower", "flow", "flight", "fpastafl"] output = longest_prefix(strings) @@ -33,7 +33,7 @@ end it "will work for the README strings" do - strings = ["dog","racecar","car"] + strings = ["dog", "racecar", "car"] output = longest_prefix(strings) From 93749e0f4d68ddf726ab9bfc9fa5535c1f8ba1c1 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 16 Sep 2019 08:50:40 -0700 Subject: [PATCH 2/5] longest_prefix completed --- lib/practice_exercises.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 78ac209..83aafc0 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,4 +1,4 @@ - +require "pry" # Time Complexity: o(n) # Space Complexity: o(1) @@ -23,27 +23,29 @@ def remove_duplicates(list) # Space Complexity: ?o(1) def longest_prefix(strings) + # If there is no common prefix, return an empty string total_words = strings.length if total_words == 0 return "" end - if total_words == 1 return strings[0] end longest_prefix = "" - total_words.times do |index| - word = strings[index] - i = index + 1 - while i < total_words - next_word = strings[i] - if word[i] == next_word[i] - longest_prefix += word[i] - break + i = 0 + strings[0].length.times do + word = strings[0][i] + common_word = 0 + strings.each do |string| + if word == string[i] + common_word += 1 end - i += 1 end + if common_word == strings.length + longest_prefix << word + end + i += 1 end return longest_prefix end From a32a689ee3dfd7514529e21319a070d13e241bff Mon Sep 17 00:00:00 2001 From: Sara Shah Baig <37054356+sarashahbaig@users.noreply.github.com> Date: Mon, 16 Sep 2019 08:57:39 -0700 Subject: [PATCH 3/5] updated --- lib/practice_exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 83aafc0..f79863e 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -20,7 +20,7 @@ def remove_duplicates(list) #============================================================== # Time Complexity: o(n^2) -# Space Complexity: ?o(1) +# Space Complexity: ?o(n) def longest_prefix(strings) # If there is no common prefix, return an empty string From c763c6f2bed7b635f0316ce9e19fabe363afb752 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig <37054356+sarashahbaig@users.noreply.github.com> Date: Mon, 16 Sep 2019 09:03:18 -0700 Subject: [PATCH 4/5] Delete practice_exercises.rb --- lib/practice_exercises.rb | 51 --------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 lib/practice_exercises.rb diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb deleted file mode 100644 index f79863e..0000000 --- a/lib/practice_exercises.rb +++ /dev/null @@ -1,51 +0,0 @@ -require "pry" -# Time Complexity: o(n) -# Space Complexity: o(1) - -# num = [1, 2, 2, 3, 4, 4] -def remove_duplicates(list) - i = 0 - while i <= list.length - if list[i] == list[i + 1] - list.delete_at(i) - end - i += 1 - end - - # print list.length - return list -end - -# remove_duplicates(num) -#============================================================== - -# Time Complexity: o(n^2) -# Space Complexity: ?o(n) - -def longest_prefix(strings) - # If there is no common prefix, return an empty string - total_words = strings.length - if total_words == 0 - return "" - end - if total_words == 1 - return strings[0] - end - - longest_prefix = "" - i = 0 - strings[0].length.times do - word = strings[0][i] - common_word = 0 - strings.each do |string| - if word == string[i] - common_word += 1 - end - end - if common_word == strings.length - longest_prefix << word - end - i += 1 - end - return longest_prefix -end From 6bba572956d1029dbcd6eaa1e1943a561e8be200 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 16 Sep 2019 12:49:19 -0700 Subject: [PATCH 5/5] Assignment done --- lib/practice_exercises.rb | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 83aafc0..a8d6ff9 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -20,32 +20,30 @@ def remove_duplicates(list) #============================================================== # Time Complexity: o(n^2) -# Space Complexity: ?o(1) +# Space Complexity: ?o(n) def longest_prefix(strings) - # If there is no common prefix, return an empty string total_words = strings.length + prefix = "" + if total_words == 0 - return "" + return prefix end + if total_words == 1 return strings[0] end - longest_prefix = "" - i = 0 - strings[0].length.times do - word = strings[0][i] - common_word = 0 - strings.each do |string| - if word == string[i] - common_word += 1 + total_words.times do |index| + word = strings[index] + i = index + 1 + while i < total_words + next_word = strings[i] + if prefix[index] != word[index] && word[index] == next_word[index] + prefix += word[index] end + i += 1 end - if common_word == strings.length - longest_prefix << word - end - i += 1 end - return longest_prefix + return prefix end