diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..086e881 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,45 @@ +require "pry" +# Time Complexity: ? O(n) where n corresponds with the length of input array +# Space Complexity: ? O(1) not creating any additional space in memory here, just deleting in place -# Time Complexity: ? -# Space Complexity: ? def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + if list.empty? || list.length == 1 + return list + else + i = 0 + j = 1 + until list[j] == nil + if list[i] == list[j] + list.delete_at(i) + end + i += 1 + j += 1 + end + return list + # raise NotImplementedError, "Not implemented yet" + end end -# Time Complexity: ? +# time complexity: ? # Space Complexity: ? + + def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + a = "" + collection_of_letters = strings.first[0] + j = 0 + + strings.each_with_index do |string, i| + + return a if string[i][j] != collection_of_letters + if j == strings.length + + a << string[i][j] + end + j += 1 + end + return a + # raise NotImplementedError, "Not implemented yet" end + diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 11d820b..f0e5f75 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -9,35 +9,44 @@ 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] + 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 Sabrina's strings" do + # strings = [“flower”,“flower”,“flower”] + + # output = longest_prefix(string) + + # expect(output).must_equal "flower" + # 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 -end +end \ No newline at end of file