From 18f3fefa1017bf5a8e7750f34282bacde87c01e7 Mon Sep 17 00:00:00 2001 From: Samantha Coll Date: Mon, 16 Sep 2019 21:59:07 -0700 Subject: [PATCH 1/3] method remove_duplicates ok --- lib/practice_exercises.rb | 45 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..d5accb7 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,44 @@ +require "pry" +# Given nums = [0,0,1,1,1,2,2,3,3,4] +# Your function should return length = 5, +# with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - depends of the length of the list +# Space Complexity: O(1) - doens't create another array def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" -end + i = 0 + while i < list.length + if list[i] == list[i+1] + list.delete_at(i) + end + i += 1 + end + return list +end + +# Find the longest common prefix string amongst an array of strings. +# Input: ["flower","flow","flight"] +# Output: "fl" +# Input: ["dog","racecar","car"] +# Output: "" -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - depends on the length of strings +# Space Complexity: O(n) - is reacreting a result total def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + total = "" + start = strings[0] + + i = 0 + start.length.times do |i| + char = start[i] + + strings.length.times do |j| + if strings[j][i] == char + total += char + end + end + end + return total end +#p longest_prefix(["flower","flow","flight]) From 9d9d9586c094734234d5c4fdc20091e1f1e46cc6 Mon Sep 17 00:00:00 2001 From: Samantha Coll Date: Mon, 16 Sep 2019 22:02:11 -0700 Subject: [PATCH 2/3] "wave2 tests failed" --- lib/practice_exercises.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index d5accb7..3f85b7b 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -41,4 +41,5 @@ def longest_prefix(strings) return total end +# Loop is not working, tests failed. #p longest_prefix(["flower","flow","flight]) From b86ad9533f3da95920e57c40ef9a9041478d96e7 Mon Sep 17 00:00:00 2001 From: Samantha Coll Date: Tue, 17 Sep 2019 18:40:56 -0700 Subject: [PATCH 3/3] fixed longest_prefix --- lib/practice_exercises.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 3f85b7b..55d72e0 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -22,24 +22,22 @@ def remove_duplicates(list) # Input: ["dog","racecar","car"] # Output: "" -# Time Complexity: O(n) - depends on the length of strings -# Space Complexity: O(n) - is reacreting a result total +# Time Complexity: O(nm) - depends on the length of strings = n and length of string[0] = m +# Space Complexity: O(m) - is reacreting a result total def longest_prefix(strings) - total = "" - start = strings[0] - + result = "" i = 0 - start.length.times do |i| - char = start[i] - - strings.length.times do |j| - if strings[j][i] == char - total += char + + strings[0].each_char do |letter| + strings.each do |string| + if letter != string[i] + return result end end + + result += letter + i += 1 end - return total + + return result end - -# Loop is not working, tests failed. -#p longest_prefix(["flower","flow","flight])