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

Angele Z. #38

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
58 changes: 52 additions & 6 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - it will always run however long the list is.
# Space Complexity: O(1) - nothing new is created
def remove_duplicates(list)

Choose a reason for hiding this comment

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

This could use a little drying up, but it works!

raise NotImplementedError, "Not implemented yet"

stored_item = nil
stored_index = 0
new_array_length = 0
current_index = 0

if list.length <= 1
return list
end

if list.length > 1
until list[current_index - 1] == list.last && stored_item != nil
if stored_item == nil
stored_item = list[current_index]
list[stored_index] = list[current_index]
new_array_length += 1
current_index += 1
stored_index += 1
elsif list[current_index] > stored_item
stored_item = list[current_index]
list[stored_index] = list[current_index]
new_array_length += 1
current_index += 1
stored_index += 1
elsif list[current_index] == stored_item
current_index += 1
end
end
end

return list[0,new_array_length]

Choose a reason for hiding this comment

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

This technically creates a new array, so it makes your space complexity O(n)


end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n*m) - or O(shortest_string.length * strings.length)
# Space Complexity: O(1) -
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
shortest_string = strings.min_by { |string| string.length }
current_index = 0

until current_index == shortest_string.length
strings.each do |string|
if string[current_index] != shortest_string[current_index]
return shortest_string[0,current_index]
end
end

current_index += 1

end

return shortest_string[0,current_index]

end

8 changes: 8 additions & 0 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
expect(output).must_equal "f"
end

it "returns the whole word if all match" do
strings = ["flower","flower","flower"]

output = longest_prefix(strings)

expect(output).must_equal "flower"
end

it "will work for the README strings" do
strings = ["dog","racecar","car"]

Expand Down