Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added longest prefix, needs work to pass test #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 71 additions & 6 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -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])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete_at takes an index number as an argument not an element. So that's why this isn't working.

delete_at also shifts each subsequent element over one index so it's an O(n) method. Since it's nested inside a loop which runs n times, this means the method would be O(n2)

Also if you delete an element you should decrement i by one so that you don't end up skipping any duplicates.

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 = ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are going to need 2 loops here one of which is looping through the characters inside one of the strings. You only have one loop here and you're comparing using an index for the array as an index for a letter in a 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]
1 change: 1 addition & 0 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "test_helper"


describe "Practice Exercises" do
describe "remove duplicates" do
it "works for 1 element strings" do
Expand Down