From 644ec63c14b658e7114a99b88c7ca670362748de Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 16 Sep 2019 00:49:14 -0700 Subject: [PATCH] Completed both waves of assignment --- lib/practice_exercises.rb | 48 ++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..f1ba4a8 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,49 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(m) +# Space Complexity: O(1) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + (list.length - 1).times do |count| + if list[count + 1] == list[count] + list[count + 1] = nil + end + end + + (list.length - 1).times do |number| + if list[number] == nil + list[number] = list[number + 1] + list[number + 1] = nil + if list[number - 1] == nil + list[number - 1] = list[number] + list[number] = nil + end + end + end + return list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n*m) +# Space Complexity: O(1) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + initial_match = '' + length = strings[0].length + length.times do |letter| + if strings[0][letter] == strings[1][letter] + initial_match.concat(strings[0][letter]) + end + end + + strings.each do |word| + match = '' + initial_match.length.times do |letter| + if initial_match[letter] == word[letter] + match.concat(word[letter]) + end + end + initial_match = match + end + + return initial_match end +# p remove_duplicates([1, 2, 2, 3, 3, 4]) +p longest_prefix(["flower","flow","flight"]) \ No newline at end of file