From f0eeed530c5504a802dabe5b67631604be145d75 Mon Sep 17 00:00:00 2001 From: Raisah Vesteinsdottir Date: Sat, 14 Sep 2019 21:47:58 -0700 Subject: [PATCH] Completed exercises and update test --- lib/practice_exercises.rb | 38 ++++++++++++++++++++++++++------- test/practice_exercises_test.rb | 31 ++++++++++++++++++--------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..60e62f7 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,35 @@ - -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: The time complexity is O(n) because the algorithms time varies linearly with the length of the list array. +# Space Complexity: O(n) where n is the length of the array. The space complexity varies with the length of the array. def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + return list if list.length == 1 || list.length == 0 + + list.length.times do |i| + if list[i] == list [i - 1] + list.delete_at(i) + end + end + + return list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: The time complexity is O(nm) because in the worst case scenario the time that the algorithm takes can change linearly based on the length of the first string in the array passed AND the length of the array itself. +# Space Complexity: O(1) because the space complexity is constant even as the array and string sizes change. def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + common_prefix = "" + i = 0 + match = true + + while match == true + value = strings[0][i] + strings.each do |s| + if s[i] != value || s[i] == nil + match = false + end + end + + common_prefix += value if match == true + i += 1 + end + + return common_prefix end - diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 11d820b..5b9c828 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -9,34 +9,45 @@ 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] 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 strings the same value" do + strings = ["flower","flower","flower"] + + output = longest_prefix(strings) + + expect(output).must_equal "flower" + output = longest_prefix(strings) + + expect(output).must_equal "flower" + end + it "will work for the README strings" do strings = ["dog","racecar","car"] - + output = longest_prefix(strings) - + expect(output).must_equal "" end end