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

Branches - Linnea #49

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
40 changes: 32 additions & 8 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@

# Time Complexity: ?
# Space Complexity: ?
require 'pry'
# Time Complexity: O(n)
# Space Complexity: O(n)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
len = list.length
if len == 1 || len == 0
return list
end

new_list = []
(0..len-1).each do |i|
if list[i] != i
new_list << list[i]
end
Comment on lines +12 to +14

Choose a reason for hiding this comment

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

This is only not adding elements if they are not equal to their index, which won't work in all cases.

Consider:

   it "will remove duplicates for even longer arrays " do
      expect(remove_duplicates([1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4])).must_equal [1, 2, 3, 4]
    end

end
return new_list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n^2)

Choose a reason for hiding this comment

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

This is actually O(nm) where m is the length of the shortest word.

# Space Complexity: O(n)

Choose a reason for hiding this comment

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

Given the above space complexity is O(m)

def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
end

result = ""
i = 0

strings[0].each_char do |letter|
strings.each do |string|
if letter != string[i]
return result
end
end

result += letter
i += 1
end
return result
end
1 change: 1 addition & 0 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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, 5, 5])).must_equal [1, 2, 3, 4, 5]
end
end

Expand Down