From 94ed6bb6d9cc694fb29aae5e63de5000027032d5 Mon Sep 17 00:00:00 2001 From: Linnea Johnson Date: Sun, 6 Oct 2019 18:51:31 -0700 Subject: [PATCH] answered complexity questions --- lib/practice_exercises.rb | 40 ++++++++++++++++++++++++++------- test/practice_exercises_test.rb | 1 + 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..d76a83c 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,37 @@ - -# Time Complexity: ? -# Space Complexity: ? +require 'pry' +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + len = list.length + if len == 1 || len == 0 + return list + end + + new_list = [] + (0..len-1).each do |i| + if list[i] != i + new_list << list[i] + end + end + return new_list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n^2) +# Space Complexity: O(n) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" -end + result = "" + i = 0 + + strings[0].each_char do |letter| + strings.each do |string| + if letter != string[i] + return result + end + end + + result += letter + i += 1 + end + return result +end \ No newline at end of file diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 11d820b..110ba99 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -12,6 +12,7 @@ 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, 5, 5])).must_equal [1, 2, 3, 4, 5] end end