diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..8713dda 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,44 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + item_list = [] + list.each_with_index do |item| + if item_list.include?(item) == false + item_list << item + end + end + return item_list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n2) +# Space Complexity: O(n) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + prefix = "" + count = 0 + check = true + continue = true + strings[0].each_char do |character| + letter = character + strings.each do |string| + if string[count] != letter + continue = false + break + end + end + if continue == false + break + end + count +=1 + end + if count == 0 + return prefix + end + return prefix = strings[0][0..count-1] end + + + + + diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 4d0bc10..0aaabc9 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]).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