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 - Brianna K #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 42 additions & 6 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -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|

Choose a reason for hiding this comment

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

This will miss the times when the number appears 3 times in a row
For example:
[1, 2, 2, 3, 4]

if list[count + 1] == list[count]
list[count + 1] = nil
end
end

(list.length - 1).times do |number|

Choose a reason for hiding this comment

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

I like how this shifts nil to the right. However it misses the problem listed above.

I went ahead and added a test for C13 on this.

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|

Choose a reason for hiding this comment

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

These two loops seem to do the roughly same thing.

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"])