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

Leaves - Cloudy #50

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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: OofNsquared bc delete_at takes up ALOT of space (so says my tutor)

Choose a reason for hiding this comment

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

Not space, but time, I understand what you're getting at.

# Space Complexity: Oof1 constant bc were not adding to it, we're subtracting
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
i = 0
list.length.times do |num|
if list[i] == list [i + 1] #if this matches its neighbor...
list.delete_at(i +1) #...delete it!
else
i += 1 #move on
end
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
#hi chris, this one boggled my mind, dont mind my notes.
# Time Complexity: OofNsquared *maybe?

Choose a reason for hiding this comment

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

That's a good idea, it's O(n * m) where n is the number of strings and m is the length of the strings.

# Space Complexity: OofN

Choose a reason for hiding this comment

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

It's probably simpler just to put O(n) ok?

You're right where n is the number of letters in a string.

def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
end
prefix = "" #where the prefix is gonna end up
i = 0 #always start here :]

while i < strings[0].length #from array - starting at the beginning of the word
j = strings[0][i] #get index of word @ letter
k = 0 #counter For same letters #lets see how far we get

strings.each do |string| #lets look and see whats up
if j == string[i] #does j[letter] match index of letter of comparable string?
k += 1 # If it does, it increments here!
end
end

if k == strings.length #comparing matches against length
prefix += j #adds letter if it finds a match
else
break #gotta exit loop
end
i += 1 #index moves on
end
return prefix #boom boom

end #end of method