diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..1b670a4 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,78 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) n being all elements in the list. They are get iterated through +#compared. +# Space Complexity: ?Would be O(1) the space stays the same throughout. No new arrays +# are created + +#compare the first number to all numbers until you reach a num thats not equal +#Then replace taht first num with num that is different, and add to the counter +#Once you reach the end of the array return the counter. + + def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + if list.length == nil + return [] + elsif list[0] == [] + return [] + end + + compare_to = list[0] + i = 0 + counter = 0 + + while list.length >= i + if compare_to == list[i] + list.delete_at(list[i]) + elsif compare_to != list[i] + counter += 1 + compare_to = list[i] + end + i += 1 + end + return list end -# Time Complexity: ? -# Space Complexity: ? + +# Time Complexity: O(n^2)Each word gets iterated through and every letter in the word +# gets iterated through. +# Space Complexity: No new Array is crated except common word. def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + + common_word = "" + strings.each_with_index do |word, index| + if strings[0][index] == word[index] + common_word += word[index] + else + return common_word + end + end + return common_word end + +# i = 0 +# common_word = "" +# #while array[0].length >= 0 +# array.each_with_index do |word, index| +# if array[0][i] == word[i] +# common_word += array[0][i] +# i +=1 +# else +# puts common_word +# end +# end +# puts "here #{common_word}" + + + +# i = 1 +# counter = 1 +# compare = strings[0] +# while strings.length >= i +# if compare[0..counter] != strings[i][0..counter] +# return "" +# else +# i += 1 +# end +# end +# return compare[0..1] \ No newline at end of file diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 4d0bc10..9de4109 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -1,5 +1,6 @@ require_relative "test_helper" + describe "Practice Exercises" do describe "remove duplicates" do it "works for 1 element strings" do